Question

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

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

#include <stdio.h>
#include <unistd.h>


int main(void)
{
FILE *fp;
int pid;
char buf[128];
int n=1;
// process id
printf("pid=%d\n", (int)getpid());
// parent process id
printf("ppid=%d\n", (int)getppid());

pid=getppid();

while(pid>1)
{
// Set the path of file to be opened in buf as mentioned in the question
sprintf(buf, "/proc/%d/stat", (int)pid);
// Open the file
fp=fopen(buf, "r");
if(fp == NULL) break;

// Ignore one number, then two strings, then read a number which the required PID
fscanf(fp, "%*d %*s %*s %d", &pid);
fclose(fp);
printf("grandparent%d id=%d\n", n++, (int)pid);
}
}

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

  • 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...

  • 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...

  • Part 2. System Programming and Process Management You are asked to develop basic system programmi...

    Can someone help me create this program for Linux. Part 2. System Programming and Process Management You are asked to develop basic system programming including process creation and termination on a Linux platform in this part of programming project. You are asked to create multiple children processes to work under one parent process. In theory, children processes can do their own work separately or cooperatively to accomplish a task. In this assignment, these children processes simply print out a "hello"...

  • Write a “C” program that creates a child process that prints out its pid and new...

    Write a “C” program that creates a child process that prints out its pid and new line and then calls pause() system call. Once it returns from the pause system call the child program must exit with code 5. The parent program must wait for the child process and print out the pid of the child process and the exit status with the following format: “childpid=%d,exitstatus=%d\n”.

  • 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 program in C what is not clear? 3. (50pts) Write a simple sequence-number system through...

    **write program in C what is not clear? 3. (50pts) Write a simple sequence-number system through which three concurrent processes, P, P2, and P3, each obtain unique integers in the range [, 500]. Use the fork) call to create P, P2, and P3. Given a file, F, containing single number, each process must perform the following steps: a a. Open F b. Read the sequence number N from the file c. Close F d. Output N and the process' PID...

  • 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...

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Use Java language to write exercise 4.20 pid manager Exercise 3.20 An operating system’s pid manager...

    Use Java language to write exercise 4.20 pid manager Exercise 3.20 An operating system’s pid manager is responsible for managing process identifiers. When a process is first created, it is assigned a unique pid by the pid manager. The pid is returned to the pid manager when the process completes execution, and the manager may later reassign this pid. Process identifiers must be unique. No two active processes may have the same pid. Use the following constants to identify 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