Question


3. (50pts) Write a simple sequence-number system through which three concurrent processes, P, P2, and P3, each obtain unique

**write program in C

what is not clear?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Code:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
const char* file_name = "read.txt";
void process()
{
   pid_t pid; //Process ID
   FILE* file = fopen (file_name, "r"); //Open file
   int N = 0;
   fscanf (file, "%d", &N); //Read Number
   fclose (file); //Close file
   if ((pid = getpid()) < 0) { //Get Process ID
       printf("unable to get pid");
   } else {
       printf("N: %d Process ID: %d",N,pid);
   }
   file = fopen(file_name, "w");
   N++;
   putw(N,file);
   fflush(file);
   fclose(file);
}
int main()
{
   if (fork() == 0) {
       printf("Starting Process A\n");
       process();
   }
   if (fork() == 0) {
       printf("Starting Process B\n");
       process();
   }
   if (fork() == 0){
       printf("Starting Process C\n");
       process();
   }
   return 0;
}

Behavior:

There are three process A, B and C running same piece of code and there is no synchronization between them and they are accessing same variable N. This leads to race condition between Processes and because of which there is unexpected value of N.

Proof:

Initial value of N is 2 in the file.

Starting Process C

Starting Process B

N: 2 Process ID: 32114

Starting Process A

N: 1 Process ID: 32456

This behavior varies from machine to machine depending on the order in which process A,B and C are executed.

Add a comment
Know the answer?
Add Answer to:
**write program in C what is not clear? 3. (50pts) Write a simple sequence-number system through...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • C program To develop a C program to implement a process system call. PROBLEM You are...

    C program To develop a C program to implement a process system call. PROBLEM You are to use the Ubuntu operating system to write a C program that creates a process to determine the identification of the current user of your computer. I mean if joe is the login of the current user of your computer your solution should return joe. Your solution must also provide the pid of both the parent and the child processes. You may use the...

  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • python/idle 1. Write a program that counts the number of A’s in a DNA sequence. The...

    python/idle 1. Write a program that counts the number of A’s in a DNA sequence. The input is one sequence in FASTA format in a file called ‘dna.txt’. For example, if the file contains: >human ACCGT then the output of the program should be 1. Your program should work for any sequence and not just the one in the example.

  • fibonacci sequence in C

    The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 i. Write a C program using the fork() system call that that generates the Fibonacci sequence by the child process. ii. If the user input as 5 then the child process has to output up to the 5 that is output must be 0,1,1,2,3,5. iii. Parent has to print the child process id and child has to print the parent process id. iv. Parent has to finish only after the child terminates. v. Is there any process synchronization problem here? Justify your answer. vi. Modify the above program to create a zombie process. How do y ou identify the zombie process? vii. Modify the above program to create an orphan process. viii. Compare and contrast the process and threads.

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork...

    Source code to modify: #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main() { pid_t pid; /*fork a child process*/ pid = fork(); if (pid<0){ /*error occured*/ fprintf(stderr,"Fork failed\n"); return 1; } else if (pid == 0) { /*child process */ printf("I am the child %d\n",pid); execlp("/bin/ls","ls",NULL); } else { /*parent process */ /*parent will wait for the child to complete*/ printf("I am the parent %d\n",pid); wait(NULL); printf("Child Complete\n"); } return 0; } 1. Write program codes for 3.21 a and...

  • You will write a C program, q1 sequence.c, that computes the value of the nth term...

    You will write a C program, q1 sequence.c, that computes the value of the nth term in any recursive sequence with the following structure: an = c1 · an−1 + c2 · an−2 a0 > 0 a1 > 0 c1 6= 0 c2 6= 0 Your C program will take 5 integer arguments on the command line: n, a0, a1, c1 and c2. n must be an integer greater than or equal to 0. If more or fewer arguments are...

  • OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of...

    OPERATING SYSTEM... C , LINUX Modify the mycat.c program to write to stderr the number of bytes that were read from stdin each time. You may use cLion inside VM, or any other text editor of your choice, such as vi or emacs. There is also a neat text editor named Kate available in Ubuntu (you can find it in the menu). In cLion you can open a single.cfile without having to create a whole new project and modify the...

  • C++ requirements The store number must be of type unsigned int. The sales value must be...

    C++ requirements The store number must be of type unsigned int. The sales value must be of of type long long int. Your program must properly check for end of file. See the section Reading in files below and also see your Gaddis text book for details on reading in file and checking for end of file. Your program must properly open and close all files. Failure to follow the C++ requirements could reduce the points received from passing the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT