C 使用管道进行过程通信
我在linux的c语言中使用管道时遇到问题。我的工作如下:C 使用管道进行过程通信,c,linux,pipe,fork,C,Linux,Pipe,Fork,我在linux的c语言中使用管道时遇到问题。我的工作如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <unistd.h> #include <fcntl.h> void myalarm(int sig) { printf("\nTime is out.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
void myalarm(int sig) {
printf("\nTime is out. Exiting...\n");
exit(0);
}
int main(int argc, char **argv){
int x, x1, n, m, pid, fd1[2];
int y = rand() % 100 + 2;
int i=0;
char *name, *arxi;
if (pipe(fd1) == -1) {
perror("ERROR: Creating Pipe\n");
exit(1);
}
pid = fork();
switch(pid) {
case -1:
perror("ERROR: Creating Child Proccess\n");
exit(99);
case 0:
close(fd1[0]); /*Writer*/
printf("\nGive a number between 1 & 100:\n");
scanf("%d", &x);
n = write(fd1[1], x, 1);
/*dup2(fd1[0],0);*/
while (x < 0 || x > 100) {
printf("ERROR:You gave a wrong number...");
printf("\nGive a number between 1 & 100:\n");
scanf("%d", &x);
}
close(fd1[1]);
break;
default:
close(fd1[1]); /*Reader*/
printf("I Am The Father Process...\n");
printf("y = %d\n", y);
m = read(fd1[0], x1, 0);
/*dup2(fd1[1],1);*/ close(fd1[0]);
if (x1 < y) {
kill(pid, SIGTERM); //termination of the child process
}
else {
signal(SIGALRM, myalarm);
printf("Please, type in your name:\n");
fflush(stdout);
alarm(10); //Start a 10 seconds alarm
scanf("%s", name);
arxi = name;
alarm(0);
while(*name != '\0') {
name++;
}
name--;
while(name >= arxi) {
putchar(*name);
name--;
}
printf("%s\n", name);
}
wait(&pid);
break;
}
return 0;
}
用c编写一个程序,它将创建一个子系统。子进程将使用管道与父进程通信。父进程将给出一个0=y之间的随机数,然后父进程将向用户询问他的名字,然后他将这样发布(George->egroeG)
我的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
void myalarm(int sig) {
printf("\nTime is out. Exiting...\n");
exit(0);
}
int main(int argc, char **argv){
int x, x1, n, m, pid, fd1[2];
int y = rand() % 100 + 2;
int i=0;
char *name, *arxi;
if (pipe(fd1) == -1) {
perror("ERROR: Creating Pipe\n");
exit(1);
}
pid = fork();
switch(pid) {
case -1:
perror("ERROR: Creating Child Proccess\n");
exit(99);
case 0:
close(fd1[0]); /*Writer*/
printf("\nGive a number between 1 & 100:\n");
scanf("%d", &x);
n = write(fd1[1], x, 1);
/*dup2(fd1[0],0);*/
while (x < 0 || x > 100) {
printf("ERROR:You gave a wrong number...");
printf("\nGive a number between 1 & 100:\n");
scanf("%d", &x);
}
close(fd1[1]);
break;
default:
close(fd1[1]); /*Reader*/
printf("I Am The Father Process...\n");
printf("y = %d\n", y);
m = read(fd1[0], x1, 0);
/*dup2(fd1[1],1);*/ close(fd1[0]);
if (x1 < y) {
kill(pid, SIGTERM); //termination of the child process
}
else {
signal(SIGALRM, myalarm);
printf("Please, type in your name:\n");
fflush(stdout);
alarm(10); //Start a 10 seconds alarm
scanf("%s", name);
arxi = name;
alarm(0);
while(*name != '\0') {
name++;
}
name--;
while(name >= arxi) {
putchar(*name);
name--;
}
printf("%s\n", name);
}
wait(&pid);
break;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
无效myalarm(内部信号){
printf(“\n时间已过。正在退出…\n”);
出口(0);
}
int main(int argc,字符**argv){
int x,x1,n,m,pid,fd1[2];
int y=rand()%100+2;
int i=0;
char*name,*arxi;
如果(管道(fd1)=-1){
perror(“错误:创建管道\n”);
出口(1);
}
pid=fork();
开关(pid){
案例1:
perror(“错误:正在创建子进程\n”);
出口(99);
案例0:
关闭(fd1[0]);/*写入程序*/
printf(“\n请输入一个介于1和100之间的数字:\n”);
scanf(“%d”和&x);
n=写入(fd1[1],x,1);
/*dup2(fd1[0],0)*/
而(x<0 | | x>100){
printf(“错误:您输入了错误的号码…”);
printf(“\n请输入一个介于1和100之间的数字:\n”);
scanf(“%d”和&x);
}
关闭(fd1[1]);
打破
违约:
关闭(fd1[1]);/*读卡器*/
printf(“我是过程之父…\n”);
printf(“y=%d\n”,y);
m=读取(fd1[0],x1,0);
/*dup2(fd1[1],1);*/close(fd1[0]);
if(x1=arxi){
putchar(*名称);
名称--;
}
printf(“%s\n”,名称);
}
等待(&pid);
打破
}
返回0;
}
可能还有其他问题,但这些问题都是错误的:
n = write(fd1[1], x, 1);
这将从地址x写入1字节,这可能会崩溃。你想要:
n = write(fd1[1], &x, sizeof(x));
类似地,这将读取0字节到地址x1,这将不起任何作用:
m = read(fd1[0], x1, 0);
你想要:
m = read(fd1[0], &x1, sizeof(x1));
因为我的英语不是很好,请给我一些c语言的东西。请编辑您的帖子,并将所有代码都放在代码格式中。@0xA3:编辑冲突,对不起。我怀疑我们也做了同样的更改(即修改代码格式),不过我也修改了标记/标题。@Konst:您现在的代码有什么问题吗?这些小部分都在一个接一个地工作,但我需要使用父子通信之间的管道通信。这是对的,谢谢……但以防万一,谢谢大家。你帮了大忙;)