在上述练习的前提下,实现能够随时收发,即
AB
可以 随时 互相收发消息:提示 用多线程 或者多
进程
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:38:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:54:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:68:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:78:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:83:5: error: break statement not within loop or switch
break;
^~~~~
3.c:88:5: error: break statement not within loop or switch
break;
^~~~~
3.c: In function ‘main’:
3.c:99:31: error: ‘callBack2’ undeclared (first use in this function); did you mean ‘callBack1’?
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~~~~~~~~
callBack1
3.c:99:31: note: each undeclared identifier is reported only once for each function it appears in
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:38:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:54:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:69:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:75:10: error: ‘buf’ undeclared (first use in this function)
bzero(buf, sizeof(buf));
^~~
3.c:75:10: note: each undeclared identifier is reported only once for each function it appears in
3.c:76:4: error: ‘res’ undeclared (first use in this function); did you mean ‘read’?
res = read(fd_r, buf, sizeof(buf));
^~~
read
3.c:80:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:39:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:55:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf’ undeclared (first use in this function)
bzero(buf, sizeof(buf));
^~~
3.c:76:10: note: each undeclared identifier is reported only once for each function it appears in
3.c:81:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:39:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:55:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf’ undeclared (first use in this function)
bzero(buf, sizeof(buf));
^~~
3.c:76:10: note: each undeclared identifier is reported only once for each function it appears in
3.c:81:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:39:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:55:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf’ undeclared (first use in this function)
bzero(buf, sizeof(buf));
^~~
3.c:76:10: note: each undeclared identifier is reported only once for each function it appears in
3.c:81:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:39:11: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:46:10: error: ‘buf1’ undeclared (first use in this function)
bzero(buf1, sizeof(buf1));
^~~~
3.c:46:10: note: each undeclared identifier is reported only once for each function it appears in
3.c:55:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf2’ undeclared (first use in this function); did you mean ‘dup2’?
bzero(buf2, sizeof(buf2));
^~~~
dup2
3.c:81:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:46:10: error: ‘buf1’ undeclared (first use in this function)
bzero(buf1, sizeof(buf1));
^~~~
3.c:46:10: note: each undeclared identifier is reported only once for each function it appears in
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf2’ undeclared (first use in this function); did you mean ‘dup2’?
bzero(buf2, sizeof(buf2));
^~~~
dup2
3.c:81:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:46:10: error: ‘buf1’ undeclared (first use in this function)
bzero(buf1, sizeof(buf1));
^~~~
3.c:46:10: note: each undeclared identifier is reported only once for each function it appears in
3.c: In function ‘callBack2’:
3.c:70:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
return -1;
^
3.c:76:10: error: ‘buf2’ undeclared (first use in this function); did you mean ‘dup2’?
bzero(buf2, sizeof(buf2));
^~~~
dup2
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
3.c: In function ‘callBack1’:
3.c:46:10: error: ‘buf1’ undeclared (first use in this function)
bzero(buf1, sizeof(buf1));
^~~~
3.c:46:10: note: each undeclared identifier is reported only once for each function it appears in
3.c: In function ‘callBack2’:
3.c:76:10: error: ‘buf2’ undeclared (first use in this function); did you mean ‘dup2’?
bzero(buf2, sizeof(buf2));
^~~~
dup2
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
1
2
3
4
5
请输入>>>请输入>>>write: Bad file descriptor
:1
read: Bad file descriptor
ubuntu@ubuntu:~/yuyu/yu/9$ 3
3: command not found
ubuntu@ubuntu:~/yuyu/yu/9$ 4
4: command not found
ubuntu@ubuntu:~/yuyu/yu/9$
ubuntu@ubuntu:~/yuyu/yu/9$ 5
5: command not found
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
请输入>>>2:1
read: Bad file descriptor
^C
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
2
^C
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread -o 3
ubuntu@ubuntu:~/yuyu/yu/9$ ./3
请输入>>>fifo2的写端关闭
^[[A^[[A^C
ubuntu@ubuntu:~/yuyu/yu/9$ ./3
:1
read: Bad file descriptor
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/9$ ./3
:2
read: Bad file descriptor
3
^C
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread -o 3
ubuntu@ubuntu:~/yuyu/yu/9$ ./3
fifo2的写端关闭
请输入>>>1
请输入>>>2
write: Bad file descriptor
ubuntu@ubuntu:~/yuyu/yu/9$ cat 3.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
int main(int argc, const char *argv[])
{
ssize_t res;
char buf1[128] = "";
char buf2[128] = "";
//创建有名管道文件
if(mkfifo("./fifo1", 0664) < 0) //向fifo1写入数据
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2", 0664) < 0) //从fifo2读取数据
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
void *callBack1(void *arg)
{
//以写的方式打开fifo1
int fd_w = open("./fifo1", O_WRONLY);
if(fd_w < 0)
{
perror("open");
return NULL;
}
// char buf[128] = "";
ssize_t res = 0;
while(1)
{
//向fifo1写入
bzero(buf1, sizeof(buf1));
printf("请输入>>>");
scanf("%s", buf1);
while(getchar() != 10); //循环吸收垃圾字符
if(write(fd_w, buf1, sizeof(buf1)) < 0)
{
perror("write");
return NULL;
}
if(strcasecmp(buf1, "quit") == 0)
break;
close(fd_w);
}
}
void *callBack2(void *arg)
{
//以读的方式打开fifo2
int fd_r = open("./fifo2", O_RDONLY);
if(fd_r < 0)
{
perror("open");
return NULL;
}
while(1)
{
//从fifo2读取
bzero(buf2, sizeof(buf2));
res = read(fd_r, buf2, sizeof(buf2));
if(res < 0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("fifo2的写端关闭\n");
break;
}
printf(":%s\n", buf2);
if(strcasecmp(buf2, "quit") == 0)
break;
close(fd_r);
}
}
pthread_t pid1;
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
{
perror("pthread_create");
return -1;
}
pthread_t pid2;
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
{
perror("pthread_create");
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
return 0;
}
ubuntu@ubuntu:~/yuyu/yu/9$
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -o 4
ubuntu@ubuntu:~/yuyu/yu/9$ ./4
:1
请输入>>>2
:2
请输入>>>1234
:3
请输入>>>1223
:4
请输入>>>1213
:4
请输入>>>2123
1223
fifo2的写端关闭
ubuntu@ubuntu:~/yuyu/yu/9$
ubuntu@ubuntu:~/yuyu/yu/9$ 1223
1223: command not found
ubuntu@ubuntu:~/yuyu/yu/9$ ./4
:1
请输入>>>2
fifo2的写端关闭
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c: In function ‘main’:
4.c:32:17: error: expected declaration specifiers or ‘...’ before ‘*’ token
void* callBack1(*arg)
^
4.c: At top level:
4.c:96:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:102:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/unistd.h:229:0,
from 4.c:5:
4.c:107:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:108:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:110:2: warning: data definition has no type or storage class
close(fd_w);
^~~~~
4.c:110:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:110:2: warning: parameter names (without types) in function declaration
4.c:111:2: warning: data definition has no type or storage class
close(fd_r);
^~~~~
4.c:111:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:111:2: warning: parameter names (without types) in function declaration
4.c:113:2: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c: In function ‘main’:
4.c:32:17: error: expected declaration specifiers or ‘...’ before ‘*’ token
void* callBack1(*arg)
^
4.c: At top level:
4.c:96:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:102:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/unistd.h:229:0,
from 4.c:5:
4.c:107:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:108:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:110:2: warning: data definition has no type or storage class
close(fd_w);
^~~~~
4.c:110:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:110:2: warning: parameter names (without types) in function declaration
4.c:111:2: warning: data definition has no type or storage class
close(fd_r);
^~~~~
4.c:111:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:111:2: warning: parameter names (without types) in function declaration
4.c:113:2: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c:96:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:102:2: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/unistd.h:229:0,
from 4.c:5:
4.c:107:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:108:20: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:110:2: warning: data definition has no type or storage class
close(fd_w);
^~~~~
4.c:110:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:110:2: warning: parameter names (without types) in function declaration
4.c:111:2: warning: data definition has no type or storage class
close(fd_r);
^~~~~
4.c:111:2: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:111:2: warning: parameter names (without types) in function declaration
4.c:113:2: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c:96:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:102:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/unistd.h:229:0,
from 4.c:5:
4.c:107:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:108:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:110:1: warning: data definition has no type or storage class
close(fd_w);
^~~~~
4.c:110:1: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:110:1: warning: parameter names (without types) in function declaration
4.c:111:1: warning: data definition has no type or storage class
close(fd_r);
^~~~~
4.c:111:1: warning: type defaults to ‘int’ in declaration of ‘close’ [-Wimplicit-int]
4.c:111:1: warning: parameter names (without types) in function declaration
4.c:113:1: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c:98:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:104:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/unistd.h:229:0,
from 4.c:5:
4.c:109:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:110:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:113:1: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c:99:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:105:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/time.h:29:0,
from /usr/include/pthread.h:24,
from 4.c:8:
4.c:110:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:111:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:112:2: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:113:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
4.c:99:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
^~
4.c:105:1: error: expected identifier or ‘(’ before ‘if’
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
^~
In file included from /usr/include/time.h:29:0,
from /usr/include/pthread.h:24,
from 4.c:8:
4.c:110:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid1,NULL);
^
4.c:111:19: error: expected ‘)’ before ‘(’ token
pthread_join(pid2,NULL);
^
4.c:112:1: error: expected identifier or ‘(’ before ‘return’
return 0;
^~~~~~
4.c:113:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
4.c:114:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
请输入>>>:1
fifo2的写端关闭
1
请输入>>>1
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
请输入>>>1
请输入>>>fifo2的写端关闭
^[[A^C
ubuntu@ubuntu:~/yuyu/yu/9$ ./a.out
1请输入>>>^C
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread -o 4
ubuntu@ubuntu:~/yuyu/yu/9$ ./4
请输入>>>1
请输入>>>2
请输入>>>fifo2的写端关闭
^C
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread -o 4
ubuntu@ubuntu:~/yuyu/yu/9$ ./4
请输入>>>1
请输入>>>:1
fifo2的写端关闭
^C
ubuntu@ubuntu:~/yuyu/yu/9$
ubuntu@ubuntu:~/yuyu/yu/9$ cat 4.c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
int main(int argc, const char *argv[])
{
ssize_t res;
char buf1[128]="";
char buf2[128]="";
//创建有名管道文件
if(mkfifo("./fifo1", 0664) < 0) //从fifo1中读取数据
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2", 0664) < 0) //向fifo2写入数据
{
if(EEXIST != errno)
{
perror("mkfifo");
return -1;
}
}
void* callBack1(void *arg)
{
//以读的方式打开fifo1
int fd_r = open("./fifo1", O_RDONLY);
if(fd_r < 0)
{
perror("open");
return NULL;
}
char buf1[128] = "";
ssize_t res = 0;
while(1)
{
//先从fifo1读取数据
bzero(buf1, sizeof(buf1));
res = read(fd_r, buf1, sizeof(buf1));
if(res < 0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("fifo2的写端关闭\n");
break;
}
printf(":%s\n", buf1);
if(strcasecmp(buf1, "quit") == 0)
break;
}
close(fd_r);
}
void *callBack2(void *arg)
{
//以写的方式打开fifo2
int fd_w = open("./fifo2", O_WRONLY);
if(fd_w < 0)
{
perror("open");
return NULL;
}
while(1)
{
//向fifo2写入数据
bzero(buf2, sizeof(buf2));
printf("请输入>>>");
scanf("%s", buf2);
while(getchar() != 10); //循环吸收垃圾字符
if(write(fd_w, buf2, sizeof(buf2)) < 0)
{
perror("write");
return NULL;
}
if(strcasecmp(buf2, "quit") == 0)
break;
}
close(fd_w);
}
pthread_t pid1;
if(pthread_create(&pid1,NULL,callBack1,NULL)<0)
{
perror("pthread_create1");
return -1;
}
pthread_t pid2;
if(pthread_create(&pid2,NULL,callBack2,NULL)<0)
{
perror("pthread_create2");
return -1;
}
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
return 0;
}
ubuntu@ubuntu:~/yuyu/yu/9$
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 3.c -pthread -o 3
ubuntu@ubuntu:~/yuyu/yu/9$ ./3
请输入>>>1
请输入>>>:2
read: Bad file descriptor
ubuntu@ubuntu:~/yuyu/yu/9$ gcc 4.c -pthread -o 4
ubuntu@ubuntu:~/yuyu/yu/9$ ./4
请输入>>>:1
fifo2的写端关闭
2
请输入>>>3
ubuntu@ubuntu:~/yuyu/yu/9$