Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Код первой программы:
- #include <stdio.h>
- #include <iomanip>
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- using namespace std;
- int f[2];
- size_t c=4;
- void* func1(void* a){
- bool*flag1=(bool*)a;
- int b=123;
- while(*flag1 ){
- ssize_t rc=write(f[1] , &b , c);
- if(rc==-1)
- {perror(" ");}
- sleep(1);
- }
- pthread_exit((void*)3);
- }
- void* func2(void* a){
- bool*flag2=(bool*)a;
- int b;
- while(*flag2){
- b=0;
- ssize_t rc=read(f[0] , &b , c);
- if(rc==-1)
- {perror(" ");}
- else{
- cout<<b<<flush;
- }
- sleep(1);
- }
- pthread_exit((void*)4);
- }
- int main(){
- bool flag1=true;
- bool flag2=true;
- pthread_t thread1,thread2;
- pipe2(f, O_NONBLOCK);
- pthread_create(&thread1, NULL, func1, &flag1);
- pthread_create(&thread2, NULL, func2, &flag2);
- getchar();
- flag1=false;
- flag2=false;
- void* st1;
- void* st2;
- pthread_join(thread1, &st1);
- pthread_join(thread2, &st2);
- close(f[0]);
- close(f[1]);
- cout<<"status 1,2:"<<((int*)st1)<<" "<<((int*)st2);
- return(0);
- }
- Код второй программы:
- #include <stdio.h>
- #include <iomanip>
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- using namespace std;
- int f[2];
- size_t c=4;
- void* func1(void* a){
- bool*flag1=(bool*)a;
- int b=123;
- while(*flag1 ){
- ssize_t rc=write(f[1] , &b , c);
- if(rc==-1)
- {perror(" ");};
- sleep(1);
- }
- pthread_exit((void*)3);
- }
- void* func2(void* a){
- bool*flag2=(bool*)a;
- int b;
- while(*flag2){
- b=0;
- ssize_t rc=read(f[0] , &b , c);
- if(rc==-1)
- {perror(" ");}
- else{
- cout<<b<<flush;
- };
- sleep(1);
- }
- pthread_exit((void*)4);
- }
- int main(){
- bool flag1=true;
- bool flag2=true;
- pthread_t thread1,thread2;
- pipe(f);
- int op= fcntl(f[0], F_GETFL);
- fcntl(f[0], F_SETFL,op|O_NONBLOCK);
- pthread_create(&thread1, NULL, func1, &flag1);
- pthread_create(&thread2, NULL, func2, &flag2);
- getchar();
- flag1=false;
- flag2=false;
- void* st1;
- void* st2;
- pthread_join(thread1, &st1);
- pthread_join(thread2, &st2);
- close(f[0]);
- close(f[1]);
- cout<<"status 1,2:"<<((int*)st1)<<" "<<((int*)st2);
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement