Question

CS4315 Operating Systems Lab 2: Linux Processes This lab assignment contains three questions. To submit this...

CS4315 Operating Systems
Lab 2: Linux Processes
This lab assignment contains three questions.
To submit this lab assignment, please use a Word document to include the
screenshots and write your answer.

1. Run the following C program, and submit a screenshot of the result.
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main( )
{
pid_t pid;
if ( (pid = fork()) == 0 )
{
printf (“I am the child, my pid = %d and my parent
pid = %d\n”, getpid(), getppid());
exit(0);
}
if (pid < 0)
{
fprintf (stderr, “fork failed\n”);
exit(1);
}
printf(“I am the parent, my pid = %d\n”, getpid());
}

2. Run the following C program, and submit the screenshot of the result. Also briefly
explain the result.
#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;
}


3. Run the following program and take a screenshot of the result. Explain the relationship
between the created processes. (Hint: if we draw an arrow that points from a process to its
child process, what shape can we get?)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
pid_t childpid = 0;
int i, nbrOfProcesses;
if (argc != 2)
{
/* Check for valid number of command-line arguments */
fprintf(stderr, "Usage: %s <processes>\n", argv[0]);
return 1;
}
/* Convert character string to integer */
nbrOfProcesses = atoi(argv[1]);
for (i = 1; i < nbrOfProcesses; i++)
{
childpid = fork();
if (childpid == -1)
{
printf("Fork failed");
exit(1);
}
else if (childpid != 0) // True for a parent
break;
} // End for
// Each parent prints this line
fprintf(stderr, "i: %d process ID: %4ld parent ID: %4ld
child ID: %4ld\n", i, (long)getpid(), (long)getppid(),
(long)childpid);
sleep(5); // Sleep five seconds
return 0;
} // End main

Homework Answers

Answer #1

1st program out put:

2nd program output:

First using fork () system call get the pid then if pid==0

Then executes the first for loop for (i=0;i<size;i++)

nums[0] =nums [0] * -i;

i.e   nums[0]=0*-0=0

it gives the out put CHILD : 0

nums[1] =nums [1] * -i;

i.e   nums[1]=1*-1=-1

it gives the out put CHILD : -1

nums[2] =nums [2] * -i;

i.e   nums[2]=2*-2=-4

it gives the out put CHILD : -4

nums[3] =nums [3] * -3;

i.e   nums[3]=3*-3=-9

it gives the out put CHILD : -9

nums[4] =nums [4] * -4;

i.e   nums[4]=4*-4=-16

it gives the out put CHILD : -16

in the second for loop it gives the output

PARENT : 0 PARENT : 1 PARENT : 2 PARENT : 3 PARENT : 4

Simply it reads the every i value and prints the i value

3 rd program output:

it terminates in the 1st condition.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using the program below, identify the values of pid at lines A, B, C, and D....
Using the program below, identify the values of pid at lines A, B, C, and D. (Assume that the actual pids of the parent and child are 2600 and 2603, respectively.)   #include <sys/types.h>   #include <stdio.h>   #include <unistd.h>      int main()   {   pid_t pid, pid1;        /* fork a child process */     pid = fork();        if (pid lt; 0) { /* error occurred */       fprintf(stderr, "Fork Failed");       return 1;     }     else if (pid == 0) { /* child process */       pid1 = getpid();       ...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A...
Run Using Kali Linux....Create a source code file named "Lab1.c". The code is as follows. A child is created and prints a message five times. The original process (parent) prints a message only three times. How would this code look on the compiler if you could please post the code and the compiler results. Need Reference Thank You #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t pid; char *message; int n; printf("fork program starting\n"); pid =...
#include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(void) {       pid_t child_pid; int   count = 6;...
#include <sys/types.h> #include <unistd.h> #include <stdio.h> int main(void) {       pid_t child_pid; int   count = 6;       child_pid = fork();       if (child_pid == 0) {             execl("/bin/ls", "ls", "-l", NULL);             ++count;             exit(1); }       else             count = count * 2;       printf ("The counter = %d \n", count);       return 0;} If execl succeeds, what will be the output for the child process? For the parent process? If execl fails, what will be the output for the child process? For...
The following program calls the fork() system call to create a child process. Suppose that the...
The following program calls the fork() system call to create a child process. Suppose that the actual pids of the parent process and child process are as follows: Parent process: 801 Child process:   802 (as returned by fork) Assume all supporting libraries have been included. => Use the answer text field to write the values of the pids printed at lines 1, 2, 3, and 4 (indicate each line number and the value printed). int main() {      pid_t pid,...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork();...
2. Calculate number of times hello is printed. #include <stdio.h> #include <sys/types.h> int main() { fork(); fork(); fork(); printf("hello\n"); return 0; }
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); }
currently the code contacts the server 1 time and exits. Modify the code so that the...
currently the code contacts the server 1 time and exits. Modify the code so that the program contacts the server five times before exiting. /* client.c - code for example client program that uses TCP */ #ifndef unix #define WIN32 #include <windows.h> #include <winsock.h> #else #define closesocket close #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #endif #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #define PROTOPORT 5193 /* default protocol port number */ extern int errno; char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...