C语言程序设计 - scanf函数不是在Ubuntu工作 [英] C Programming - Scanf not working in ubuntu

查看:502
本文介绍了C语言程序设计 - scanf函数不是在Ubuntu工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在Ubuntu 10 C程序创建进程,显示进程ID和杀死进程。我使用kill()命令来杀死用户通过scanf函数输入的进程ID。然而,scanf函数是不工作的。我尝试%d个前添加空间,但什么都没有发生。鸭preciate如果有人能帮助!

以下是我的codeS:

 包括<&stdio.h中GT;
包括<&unistd.h中GT;
包括<&signal.h中GT;
包括< SYS / types.h中>
包括<&stdlib.h中GT;main()的{
    INT X;
    INT PID [10]; //存储叉返回值
    INT P [10]; //存储进程ID    //创建5个新的流程和其进程ID存储
    为(X = 1; X&下; = 5; X ++)    {
        如果((PID [X] =叉())== 0)        {
            P [X] = GETPID();            的printf(\\ n我的过程:%d个,我的进程ID:%d个\\ N,X,P [X]); //显示进程号和PID
        }
        其他
            出口(0);
    }    {
        诠释Ÿ;
        Y = P [X];
        的printf(请输入一个进程ID杀:);
        scanf函数(%d个,&安培; Y); //等待用户输入
        的printf(\\ n此过程%d被杀死\\ n,Y);
        杀(Y,9); //值9重presents SIGKILL    }}


解决方案

您的父进程退出,所以不会每次产卵后过程(叉他们的返回值大于1,因此他们退出不同)。如果一个进程没有父它会变成一个孤儿,并通过操作系统(其他一些工艺采用的话)具有特殊处理。你确定这是你要找的人?

行为

编辑:
这可能是你的意思写的:

 的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&signal.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&;&stdlib.h中GT;诠释的main()
{
INT X;
INT PID [10]; //存储叉返回值将为pid_t父= GETPID();
//创建5个新的流程和其进程ID存储
为(X = 1; X&下; = 5; X ++){
    如果((PID [X] =叉())!= 0)    {
        的printf(\\ n我生成的进程:%D,它的进程ID:%d个\\ N,X,PID [X]); //显示进程号和PID
    }其他{
            而(1){}
    }
}如果(GETPID()==父){
    诠释Ÿ;
    Y = PID [X];
    的printf(请输入一个进程ID杀:);
    scanf函数(%d个,&安培; Y); //等待用户输入
    的printf(\\ n此过程%d被杀死\\ n,Y);
    杀(Y,9); //值9重presents SIGKILL}其他{
    的printf(这不应该发生!);
}
  返回0;
}

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;
}

这篇关于C语言程序设计 - scanf函数不是在Ubuntu工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆