C Program using UNIX System Call


/* illustrating IPC through pipe and fork system calls – Printing only odd numbers */

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

int main()

{

int pfd[2], i;

pid_t mypid;

if(pipe(pfd) < 0)

perror(“Pipe Error”);

if(!fork())

{

char data;

printf(“Enter a Number…\n”);

scanf(“%d”, &data);

write(pfd[1], &data, 1);

mypid = getpid();

printf(“I am process %d\n”, mypid);

printf(“My parent is process %d\n”, getppid());

printf(“Child Exiting…\n”);

exit(0);

}

else

{

char data1;

read(pfd[0], &data1, 1);

printf(“Received %d from child \n”, data1);

printf(“The odd numbers are… \n”);

for(i=1; i<=data1; i+=2)

{

printf(“%5d”, i);

sleep(2);

}

printf(“\n Parent Exiting…\n”);

exit(0);

}

return(0);

}

Advertisements

3 thoughts on “C Program using UNIX System Call

  1. wat about grep simulation ………..

    program for memory management and file allocation technique please

Comments are closed.