Why Isn't My Linux Child Process Program Working?

In summary, the conversation discusses a program that creates and destroys child processes based on user input. The speaker is struggling with an issue in the code and suggests providing more specific information about the problem.
  • #1
khdani
55
0
Hello,
I try to make a program which gets the number of desired childs from user, then creates them sequentially and destroys them in reverse order.
I just don't understand why it doesn't work correctly :(

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char** argv) {
        int pid, status,n_sons,current_son=0;
        printf("Enter number of sons:");
        scanf("%d",&n_sons);

        do {
                pid=fork();
                if(pid !=0 ) {
                        printf("Created Son, with PID:%d and PPID:%d\n",pid,getppid());
                        wait(&status);
                        printf("Son with PID:%d, terminated with status:%d\n",pid,status);
                        break;
                }
                else {
                       current_son++;
                }
        }while(current_son < n_sons);

        return 0;
}
 
Technology news on Phys.org
  • #2
found the problem
 
  • #3


I would first commend the individual for their efforts in creating a program and trying to understand why it is not working correctly. It takes perseverance and determination to troubleshoot and solve problems in programming.

In terms of the program itself, there could be several reasons why it is not working correctly. Without seeing the specific errors or issues the individual is encountering, it is difficult to provide a specific response. However, there are some general suggestions that could help in troubleshooting and improving the program.

Firstly, it is important to thoroughly test the program and identify any specific errors or issues that are occurring. This could involve running the program with different inputs and analyzing the output and any error messages that are displayed.

Secondly, it may be beneficial to add some error handling and debugging statements to the program. This can help identify where the program is encountering issues and provide more information for troubleshooting.

Additionally, it may be helpful to review the logic of the program and ensure that it is correctly creating and destroying the desired number of child processes. This could involve reviewing the loop and conditional statements used to create and terminate the processes.

Finally, seeking assistance from others who have experience with programming and specifically with creating and managing processes in Linux may also be beneficial in finding a solution to the issue. Collaboration and seeking help from others is an important aspect of being a scientist and can often lead to successful problem-solving.
 

FAQ: Why Isn't My Linux Child Process Program Working?

What is a Linux process?

A Linux process refers to a running instance of a program or application in the Linux operating system. It is a fundamental concept in the Linux environment, where everything is considered as a process.

How do I view all running processes in Linux?

To view all running processes in Linux, you can use the command "ps -aux" in the terminal. This will display a list of all processes running on your system, along with their corresponding process IDs (PID) and other information such as CPU and memory usage.

How can I terminate a process in Linux?

You can terminate a process in Linux using the "kill" command followed by the process ID (PID) of the process you want to terminate. For example, the command "kill 1234" will terminate the process with the PID 1234.

Can I run multiple processes in Linux at the same time?

Yes, you can run multiple processes in Linux at the same time. This is one of the advantages of using a multitasking operating system like Linux. Each process is allocated its own resources and runs independently from other processes.

What are the different states a process can have in Linux?

In Linux, a process can have one of the following states: running, waiting, sleeping, stopped, zombie. A running process is actively using the CPU, a waiting process is waiting for a resource, a sleeping process is temporarily inactive, a stopped process has been stopped by a user or the system, and a zombie process has completed its execution but still has an entry in the process table.

Back
Top