Question

To be done in C for LINUX. Write a simple test program to find out the...

To be done in C for LINUX.

Write a simple test program to find out the parent process id of a process before and after its parent process terminates. Based on your test results, answer the following question: what is a process's parent process after its original parent process terminates?

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

// code

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define MAX 10
  
int main()
{
char turn = 'c';
int fd[2];
  
pipe(fd);
  
pid_t child_id = fork();
  
if(child_id > 0)
{
// Parent: reading only, so close the write-descriptor
close(fd[1]);
  
while(turn == 'c')
{

// now read the data (will block)
read(fd[0], &turn, sizeof(turn));
// close the read-descriptor
close(fd[0]);
}
  
  
printf("Parent is exiting");
}
else if(child_id == 0)
{
close(fd[0]);
pid_t parent_id = getppid();
pid_t new_parent_id = 0;
  
  
printf("\n Parent id is: %d", parent_id);
turn = 'p';
  
write(fd[1], &turn, sizeof(turn));
  
close(fd[1]);
  
printf("\nTurn is: %c", turn);
new_parent_id = getppid();
printf("\nNew parent_id: %d", new_parent_id);
}
else
{
perror("error\n"); //fork()
}
}

// After parent exit parent is now init that is pid = 1;

Add a comment
Know the answer?
Add Answer to:
To be done in C for LINUX. Write a simple test program to find out the...
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
  • For Linux Operating System: Write a simple test program in C to find out PIDs of...

    For Linux Operating System: Write a simple test program in C to find out PIDs of ALL ancestor (i.e., parent, grandparent ) processes of the current process or a given process specified by a PID. A parent process's PID of a given process pid can be found in /proc/$pid/status

  • LINUX! In shell scripting if possible and show commands (please do not copy codes form others)...

    LINUX! In shell scripting if possible and show commands (please do not copy codes form others) Thank you! Write a simple test program to find out PIDs of ALL ancestor (i.e., parent, grandparent ) processes of the current process or a given process specified by a PID. A parent process's PID of a given process pid can be found in /proc/$pid/status

  • a) Write a C-program that creates a chain of 10 processes and prints out their process...

    a) Write a C-program that creates a chain of 10 processes and prints out their process ids and relationships. For example, process 1 is the parent of process 2, process 2 is the parent of process 3, process 3 is the parent of 4 and so on. Each child has to print out all her ancestors identified by the process ids. b) Write a C-program that creates a fan of 10 processes. That is, process 1 is the parent of...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • Using either a UNIX or a Linux system, write a C program that forks a child...

    Using either a UNIX or a Linux system, write a C program that forks a child process that ultimately becomes a zombie process. Process states can be obtained from the command: ps -l The process states are shown below the S column; processes with a state of Z are zombies. The process identifier (pid) of the child process is listed in the PID column, and that of the parent is listed in the PPID column. Because you do not want...

  • OK, write a "hello, world!" C program. Keep it simple. On the Linux box, use cc...

    OK, write a "hello, world!" C program. Keep it simple. On the Linux box, use cc to convert the C source to Assembler source. Suppose nobody's told you how to do that, but some kind soul mentioned the "man" (Unix Programmer's Manual) database. You'll be using the cc command, so what does the database have to say about cc? Find your own answers, this is real-world. After you have the Assembler Source, see if you can assemble and link it...

  • How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can...

    How to Write a C program (for Linux/Unix) that: –Creates a struct, where each instance can store one message. –Forks a process. –Creates 2+ messages, with contents of your choice, in the parent and sends them to the child via a named or anonymous pipe. –Prints the contents of all received messages in the child process. –If necessary, cleans up after the pipe. Note (the subjects about signal and Pipes )

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • Questions: 1) Write a simple program to create three processes using fork() commands. Use any three...

    Questions: 1) Write a simple program to create three processes using fork() commands. Use any three of the six system calls to show how each child process executes new sub-programs using exec’s minions. Show the output of each of the child process with different data samples.   2) Write a simple program in your Linux environment using C++ to identify PID, PPID and GID for the processes created in Question 1 and display the real and effective user ID.   3) Modify...

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