2016-06-30 5 views
0

このコードは、1つの名前付きパイプで動作する2つの異なるプロセス(同じフォルダ内に存在)に2つのプロセスがあります。 第1のプログラムは、リストを通って連続的に通過するポインタを有する。私の別のプログラム(timer.c)から 'timer interrupt'が受信されると、ポインタは停止しなければならず、その特定のリストは削除されます。は、2つのプロセス間で名前付きパイプを使用して連続的に通信します。

これらは私のコードです:

ball.c

#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <limits.h> 
#include <string.h> 
#include <unistd.h> 

#define FIFO_NAME "my_fifo" /* the fifo name */ 

#define BUFFER_SIZE 10 


struct node 
{ 
int data; 
struct node* next; 
}; 


struct node* create_new() 
{ 
struct node* temp = (struct node *)malloc(sizeof(struct node)*1); 
return temp; 
}; 


void populate_list(struct node **head, int players) 
{ 
struct node *current, *temp; 
int i = 1; 
temp = create_new(); 
temp->data = i++; 
temp->next = temp; 
*head = temp; 
while(i <= players){ 
    current = create_new(); 
    current->data = i; 
    current->next = *head; 
    temp->next = current; 
    temp = current; 
    i++; 
} 
} 


void display_list(struct node **head) 
{ 
if(NULL == *head) 
{ 
    printf("the list is empty\n"); 
} 
else 
{ 
    struct node *temp = *head; 
    while(temp->next != *head){ 
     printf("%d - ", temp->data); 
     temp = temp->next; 
    } 
    printf("%d\n", temp->data); 
} 
} 


void delete_player(struct node **pos) 
{ 
printf("Deleting Player - '%d'\n", (*pos)->data); 
struct node *temp, *ptr; 
temp = ptr = *pos; 
while(temp->next != *pos){ 
    temp = temp->next; 
} 
temp->next = ptr->next; 
free(ptr); 
*pos = temp; 
} 



int main(int argc, char **argv) 
{ 
int res; 
char buffer[BUFFER_SIZE + 1]; 
if (access(FIFO_NAME, F_OK) == -1)  /* check if fifo already exists*/ 
{ 
    res = mkfifo(FIFO_NAME, 0777); /* if not then, create the fifo*/ 
    if (res != 0) { 
     fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); 
     exit(EXIT_FAILURE); 
    } 
} 

memset(buffer, '\0', BUFFER_SIZE+1);  /* clear the string */ 

printf("Process %d - opening FIFO\n\n", getpid()); 
res = open(FIFO_NAME, O_RDWR | O_NONBLOCK); 

struct node *head = NULL; 
int players; 

printf("Enter the number of players: "); 
scanf("%d", &players); 

populate_list(&head, players); 
printf("\nThe players are: \n"); 
display_list(&head); 

printf("\n-------------------Game Started-----------------\n"); 

struct node *pos, *start = head; 
int breakflag = 0; 

while(start->next != start) 
{ 
    while(breakflag == 0) 
    { 
     read(res, buffer, BUFFER_SIZE+1); 
     if(strcmp(buffer, "intr") == 0){ 
      breakflag = 1; 
      memset(buffer, '\0', BUFFER_SIZE+1); 
     } 
     start = start->next; 
    } 
    pos = start; 
    start = start->next; 
    printf("\nThe ball is currently with Player '%d'\n", pos->data); 
    delete_player(&pos); 
    display_list(&pos); 
    breakflag = 0;  //Restart the passing game 
} 

printf("\nWinner: Player '%d'\n", start->data); 
free(start); 

write(res, "stop", strlen("stop")+1); 

if(res != -1) 
    (void)close(res); /* close the fifo */ 

printf("Process %d - ballpass.c - finished\n", getpid()); 

return 0; 
} 

timer.c

#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <limits.h> 

#define FIFO_NAME "my_fifo" /* the fifo name */ 
#define FIFO_NAME1 "my_fifo1" /* the fifo name */ 
#define BUFFER_SIZE 10 

/* Used to generate the timer delay */ 
void waitFor(unsigned int secs) 
{ 
unsigned int retTime = time(0) + secs; 
while(time(0) < retTime); 
} 

int main() 
{ 
int res, i, random_time; 
char buffer[BUFFER_SIZE + 1]; 

if (access(FIFO_NAME, F_OK) == -1) { /* check if fifo already exists*/ 
    res = mkfifo(FIFO_NAME, 0777); /* if not then, create the fifo*/ 
    if (res != 0) { 
     fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); 
     exit(EXIT_FAILURE); 
    } 
} 

memset(buffer, '\0', BUFFER_SIZE+1); /* clear the string */ 

printf("Process %d - opening FIFO\n\n", getpid()); 
res = open(FIFO_NAME, O_RDWR | O_NONBLOCK); 

while(1) 
{ 
    read(res, buffer, BUFFER_SIZE+1); 
    if(strcmp(buffer, "stop") == 0) 
     break; 

    random_time = rand()%10; 
    waitFor(random_time); 

    write(res, "intr", strlen("intr")+1); 

    printf("Process %d - Timer sent interrupt\n"); 
} 

if (res != -1) 
    (void)close(res); /* close the fifo */ 

printf("Process %d - timer.c - finished\n", getpid()); 

exit(EXIT_SUCCESS); 
} 

問題は私の第一progはタイマーをキャッチすることができないからです正しく。私のタイマープログラムは、1stプログラムが送信する「停止」も受信しません。したがって、同期が全くありません。 タイマーから2つ以上の '割り込み'が受信された後、1番目のプログがノードを捕捉して削除します。

私はここで何が欠けていますか?

答えて

1

Linuxを使用している場合は、名前付きパイプ(fifos)と名前のないパイプ(シェル "|")も一方向です。双方向通信が必要な場合は、2番目の名前付きパイプを使用するか、ソケットペアなどの別の通信ツールに変更する必要があります。

また、パイプを使用している場合は、ブロックモードで開くことをお勧めします。これにより、両方のプログラム(クライアントとサーバー)がopen()呼び出しを超え、両方がパイプに接続している場合にのみ確実に実行されます。それ以外の場合、ライターがリーダーの前に接続すると、一部のデータが失われる可能性があります(私はこれについて完全にはわかりません)。

これらのリンクは、FIFOとsockerpairsについての詳細を知っておくと便利かもしれません:

:適切にFIFOを使用する方法 http://linux.die.net/man/2/socketpair
  • 関連する問題