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();       ...
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; }
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 +...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
0001 #include <stdio.h> 0002 0003 #define NUM_PRODUCTS 2 0004 #define NUM_CATEGORIES 2 0005 #define NUM_DIGITS 2...
0001 #include <stdio.h> 0002 0003 #define NUM_PRODUCTS 2 0004 #define NUM_CATEGORIES 2 0005 #define NUM_DIGITS 2 0006 0007 struct ProductInfo 0008 { 0009     int prodID; 0010     int numberInStock; 0011     double unitPrice; 0012 }; 0013 0014 int main(void) 0015 { 0016     struct ProductInfo productList[NUM_PRODUCTS] = 0017     { 0018         {78, 8, 19.95}, 0019         {95, 14, 22.98} 0020     }; 0021 0022     int categoryCount[NUM_CATEGORIES] = { 0 }; 0023 0024     int i,...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT