2010-12-05 3 views
0
/*Write a server application that does the following : 
open 5 named pipes in read­only mode & read from each in a separate thread created in your process – meaning, one thread per named pipe ; main threadoes not read from a named pipe, instead joins all the threads and  
terminates; each thread uses its own individual buffer for reading data from  
a specific named pipe 
*/ 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include<pthread.h> 
#include<fcntl.h> 
void* thread_routine1(void*fd1) 
{ 
char buff[512]; 
int bytes_read; 
while((bytes_read = read((int)fd1,buff,20)) >0) 
    { 
    printf("%s\n", buff); 
        write(stdout,buff,bytes_read); 
    //fflush(stdout); 
    } 
    if(bytes_read<0){perror("error"); } 
    close((int)fd1); 
} 
void* thread_routine2(void*fd2) 
{ 
char buff[512]; 
int bytes_read; 
while((bytes_read = read((int)fd2,buff,512)) >0) 
    { 
    //printf("%s\n", buff); 
        write(STDOUT_FILENO,buff,bytes_read); 
    //fflush(stdout); 
    } 
    if(bytes_read<0){ } 
    close((int)fd2); 
} 
void* thread_routine3(void*fd3) 
{ 
char buff[512]; 
int bytes_read; 
while((bytes_read = read((int)fd3,buff,512)) >0) 
    { 
    //printf("%s\n", buff); 
        write(STDOUT_FILENO,buff,bytes_read); 
    //fflush(stdout); 
    } 
    if(bytes_read<0){ } 
    close((int)fd3); 
} 
void* thread_routine4(void*fd4) 
{ 
char buff[512]; 
int bytes_read; 
while((bytes_read = read((int)fd4,buff,512)) >0) 
    { 
    //printf("%s\n", buff); 
        write(STDOUT_FILENO,buff,bytes_read); 
    //fflush(stdout); 
    } 
    if(bytes_read<0){ } 
    close((int)fd4); 
} 
void* thread_routine5(void*fd5) 
{ 
char buff[512]; 
int bytes_read; 
while((bytes_read = read((int)fd5,buff,512)) >0) 
    { 
    //printf("%s\n", buff); 
        write(STDOUT_FILENO,buff,bytes_read); 
    //fflush(stdout); 
    } 
    if(bytes_read<0){ } 
    close((int)fd5); 
} 
int main() 
{ 
    pthread_t thid[5]; 
int status,ret; 
int fd1,fd2,fd3,fd4,fd5; 
    int res = mkfifo("/tmp/my_fifo1", 0777); 
    if (res == 0) 
     printf("FIFO 1created\n"); 
    if(res<0) 
    perror("FIFO already created"); 
    res = mkfifo("/tmp/my_fifo2", 0777); 
    if (res == 0) 
     printf("FIFO created\n"); 
    if(res<0) 
    perror("FIFO 2already created"); 
     res = mkfifo("/tmp/my_fifo3", 0777); 
    if (res == 0) 
     printf("FIFO created\n"); 
    if(res<0) 
    perror("FIFO 3already created"); 
     res = mkfifo("/tmp/my_fifo4", 0777); 
    if (res == 0) 
     printf("FIFO created\n"); 
    if(res<0) 
    perror("FIFO 4already created"); 
     res = mkfifo("/tmp/my_fifo5", 0777); 
    if (res == 0) 
     printf("FIFO created\n"); 
    if(res<0) 
    perror("FIFO5 already created"); 
    fd1=open("/tmp/my_fifo1",O_RDONLY); 
    perror("open status is"); 
    sleep(5); 
    fd2=open("/tmp/my_fifo2",O_RDONLY); 
     perror("open status is"); 
    fd3=open("/tmp/my_fifo3",O_RDONLY); 
     perror("open status is"); 
    fd4=open("/tmp/my_fifo4",O_RDONLY); 
     perror("open status is"); 
    fd5=open("/tmp/my_fifo5",O_RDONLY); 
     perror("open status is"); 

     ret=pthread_create(&thid[0],NULL,thread_routine1,(void*)&fd1); 
     if(ret<0) 
     perror("error in thread create1"); 
    ret=pthread_create(&thid[4],NULL,thread_routine4,(void*)&fd5); 
     if(ret<0) 
     perror("error in thread create2"); 
     ret=pthread_create(&thid[1],NULL,thread_routine3,(void*)&fd2); 
     if(ret<0) 
     perror("error in thread create3"); 
     ret=pthread_create(&thid[2],NULL,thread_routine2,(void*)fd3); 
     if(ret<0) 
     perror("error in thread create4"); 
     ret=pthread_create(&thid[3],NULL,thread_routine5,(void*)fd4); 
     if(ret<0) 
     perror("error in thread create5"); 
    ret=pthread_join(thid[0],NULL); 
     ret=pthread_join(thid[1],NULL); 
     ret=pthread_join(thid[2],NULL); 
      ret=pthread_join(thid[3],NULL); 
       ret=pthread_join(thid[4],NULL); 
    ret=pthread_join(thid[5],NULL); 
    } 



    the above code for a process that creates a named pipe FIFO 
#include<fcntl.h> 
#include<sys/types.h> 
#include<sys/stat.h> 
#include<unistd.h> 
#include<stdlib.h> 
#include<stdio.h> 
#include<strings.h> 


int main(int argc, char *argv[]) 
{ 

    char buf[512],wr_buf[512]; 
    int ret,ret1,status; 
    int npfd1,npfd2,npfd3,npfd4,npfd5; 
    struct stat s1,s2; 

    // ret = mkfifo(argv[1],0600); 


    npfd1 = open("/tmp/my_fifo1", O_WRONLY); //opening the named pipe for reading 
    npfd2= open("/tmp/my_fifo2", O_WRONLY); //opening the named pipe for reading 
    npfd3 = open("/tmp/my_fifo3", O_WRONLY); //opening the named pipe for reading 
     npfd4 = open("/tmp/my_fifo4", O_WRONLY); //opening the named pipe for reading 
     npfd5 = open("/tmp/my_fifo5", O_WRONLY); //opening the named pipe for reading 
            //only 
    if(npfd1<0) {perror("error in opening the named pipe"); exit(1); } 
printf("enter someting into the buffer\n"); 
bzero(buf,sizeof(buf)); 
gets(buf); 
    ret1 = write(npfd1,buf,20); 

    //printf("%s\n", buf); 
        write(STDOUT_FILENO,buf,ret1); 
    //fflush(stdout); 

    if(ret1<0){ printf("nothing is there\n");} 
    close(npfd1); 

    exit(0); 

} 

위 코드는 FIFO의 쓰기 끝을 생성하는 위치입니다. 이제 다른 터미널에서 두 프로그램을 실행 한 후 잘못된 파일 설명자 오류가 나타납니다. 내가 처음 namedpipe에 뭔가 쓰려고 할 때. my_fifo1. 메인에서 열리는 filedescriptor를 스레드 방식으로 전달할 수 있습니까?잘못된 파일 설명자가 스레드에 realted

+2

나는 지금 실제로 그것을 읽을 수 있도록 게시물을 편집했습니다. 답변을 시도하는 사람이 실제로 읽을 수있는 글을 게시하고 올바른 문법을 사용하도록 시간을내어주십시오. (영어가 모국어가 아니어도 10 개의 물음표를 연속으로 넣는 것이 끝나지 않는 것이 좋습니다. 질문). – Yuliy

+0

좋아, 다음 번엔 신경 써라. –

답변

2

main() 함수가 포인터를 int로 전달하지만 스레드 함수가 int으로 캐스팅 한 다음 파일 설명자로 사용하려고합니다. 그게 작동하지 않을거야 - 스레드 함수에서 전달 된 포인터를 참조 해제해야합니다 (이 경우에는 main()의 변수가 스레드 당 하나씩 할당되고 모든 스레드가 완료 될 때까지 main()이 종료되지 않기 때문에이 경우 OK입니다.).

void *thread_routine1(void *fd1) 
{ 
char buff[512]; 
int bytes_read; 
int fd = *(int *)fd1; 

while ((bytes_read = read(fd,buff,20)) > 0) 

/* ... */ 
관련 문제