Starting from:

$25

Comp307 - LAB-1  - Understanding Process Concepts - Solved

1.    Write a C program to implement process System calls ? [Hint use fork())]

2.    How many processes are created in a given program?

#include <stdio.h> #include <unistd.h> int main()

{ int i; for (i = 0; i < 4; i++) fork(); return 0;

}

3.    When will line J be reached?

#include <sys/types.h>

#include <stdio.h> #include <unistd.h> int main()

{ pid_t pid;

/* fork a child process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1;

} else if (pid == 0) { /* child process */ execlp("/bin/ls","ls",NULL); printf("LINE J");

} else { /* parent process */

/* parent will wait for the child to complete */ wait(NULL); printf("Child Complete");

} return 0;

}

4. What are the pid values?

#include <sys/types.h>

#include <stdio.h> #include <unistd.h> int main()

{ pid_t pid, pid1; /* fork a child process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1;

} else if (pid == 0) { /* child process */ pid1 = getpid(); printf("child: pid = %d",pid); /* A */ printf("child: pid1 = %d",pid1); /* B */

} else { /* parent process */ pid1 = getpid(); printf("parent: pid = %d",pid); /* C */ printf("parent: pid1 = %d",pid1); /* D */ wait(NULL);

} return 0;

}

5. What will be at Line X and Line Y?

#include <sys/types.h>

#include <stdio.h>

#include <unistd.h> #define SIZE 5 int nums[SIZE] = {0,1,2,3,4}; int main()

{ int i; pid t pid; pid = fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums[i] *= -i; printf("CHILD: %d ",nums[i]); /* LINE X */ } } else if (pid > 0) { wait(NULL);

for (i = 0; i < SIZE; i++) printf("PARENT: %d ",nums[i]); /* LINE Y */

} return 0;

}

More products