I/O System Calls > C Program
Operating Systems
I/O System Calls > C Programs
Program 1
#include<stdio.h>#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<errno.h>
int main(int argc,char*argv[])
{
pid_t pid;
pid=fork();
printf("1st fork\n");
printf("Process Id:%d,Parent ProcessId:%d\n",getpid(),getppid());
printf("2nd fork\n");
printf("Process Id:%d,Parent ProcessId:%d\n",getpid(),getppid());
printf("3rd fork\n");
printf("Process Id:%d,Parent ProcessId:%d\n",getpid(),getppid());
return 0;
}
/*
OUTPUT----------
[root@Server Desktop]# gcc systemcall.c
[root@Server Desktop]# ./a.out
1st fork
Process Id:3622,Parent ProcessId:3621
2nd fork
Process Id:3622,Parent ProcessId:3621
3rd fork
Process Id:3622,Parent ProcessId:3621
1st fork
Process Id:3621,Parent ProcessId:3567
2nd fork
Process Id:3621,Parent ProcessId:3567
3rd fork
Process Id:3621,Parent ProcessId:3567
[root@Server Desktop]# ./a.out
1st fork
Process Id:3624,Parent ProcessId:3623
2nd fork
Process Id:3624,Parent ProcessId:3623
3rd fork
Process Id:3624,Parent ProcessId:3623
1st fork
Process Id:3623,Parent ProcessId:3567
2nd fork
Process Id:3623,Parent ProcessId:3567
3rd fork
Process Id:3623,Parent ProcessId:3567
*/
Program 2
#include<sys/stat.h>
#include<fcntl.h>
#include<sys/types.h>
#include<stdio.h>
int main()
{
int n, i=0;
int f1, f2;
clrscr();
char c, strin[100];
f1=open("data",O_RDWR|O_CREAT|O_TRUNC);
c=getchar();
while(c!='\n') {
strin[i++]=c;
}
strin[i]='\0';
write(f1, strin, i);
close(f1);
f2=open("data",O_RDONLY);
read(f2, strin, 0);
printf("\n%s\n",strin[i]);
close(f2);
return 0;
}
Output:
Hello
Hello
Comments
Post a Comment