简体   繁体   中英

C Programming - Scanf not working in ubuntu

I am writing a C program in Ubuntu 10 to create processes, display process ID and to kill process. I'm using kill() command to kill a process ID that user entered via scanf. However, the scanf is not working at all. I tried to add "space" before %d but nothing happened. Appreciate if anyone can help!

Following are my codes:

include <stdio.h>
include <unistd.h>
include <signal.h>
include <sys/types.h>
include <stdlib.h>

main ()

{   
    int x;
    int pid[10]; // to store fork return value
    int p[10]; // to store process ID

    // Create 5 new processes and store its process ID
    for (x=1;x<=5;x++)

    {
        if ((pid[x]=fork())==0)

        {
            p[x]=getpid();

            printf("\n I am process: %d, my process ID: %d\n",x,p[x]); // Display process number and PID
        }
        else
            exit(0);
    }

    {   
        int y;      
        y=p[x];
        printf("Please enter a process ID to kill: "); 
        scanf(" %d", &y); //waiting for user input
        printf("\nThe process %d is killed.\n",y);
        kill(y,9); //Values 9 represents SIGKILL

    }

}

Your parent process exits, and so does every process you spawn afterwards (their return value of fork is different than 1 so they exit). If a process has no parent it becomes an "orphan" and has special handling by the OS (some other process adopts it). Are you sure this is the behavior you were looking for?

EDIT: This is probably what you meant to write:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <stdlib.h>

int main ()
{
int x;
int pid[10]; // to store fork return value

pid_t parent = getpid();
// Create 5 new processes and store its process ID
for (x=1;x<=5;x++)

{  
    if ((pid[x]=fork())!=0)

    {  
        printf("\n I spawned process: %d, its process ID: %d\n",x,pid[x]); // Display process number and PID
    }else{ 
            while(1){}
    }
}

if(getpid() == parent){
    int y;
    y=pid[x];
    printf("Please enter a process ID to kill: ");
    scanf(" %d", &y); //waiting for user input
    printf("\nThe process %d is killed.\n",y);
    kill(y,9); //Values 9 represents SIGKILL

}else{
    printf("THIS SHOULD NOT HAPPEN!");
}
  return 0;
}

fork returns twice, each time in a different process. One very important thing to realize about the two processes is that they do not share memory. That means that by calling getpid in the child and saving that in an array you are unable to see that value in the parent's copy of the variable.

What you most likely want to do is something like:

for (...) {
  if ((pid[x]=fork()) == 0) {
    printf("child created, pid = %d\n", getpid());
    while(1) sleep(1000); /* children will never run outside this loop */
  } else {
    continue;
  }
}
/* this code only runs in the parent */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM