You are on page 1of 2

CS 311 Operating Systems

Quiz – 1

Name:
Reg No:
Section:

Q1: The getpid() function returns the process ID of the process calling it. What is the
output of the following code [4]?

Assume that Parent PID is 1337 and Child PID is 3142

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void) {
int childpid;
int mypid;
childpid = fork();
if (childpid == -1) {
perror("Failed to fork");
return 1;
}
mypid = getpid();
if (childpid == 0)
printf("I am child %d, ID = %d\n", getpid(), mypid);
else
printf("I am parent %d, ID = %d\n",getpid(), mypid);
return 0;
}

I am child 3142, ID = 3142

I am parent 1337, ID = 1337

Q2: Write the code for a program that creates a child process to run the following program
[6]:

ps -x -u

Hint: Use execvp() and wait()


args[0] = firstargument (name of the program)
args[1] = secondargument (first argument to the program)
args[2] = thirdargument (second argument to the program)
args[3] = NULL

execvp(args[0] OR “name of the program”, args)

You might also like