开发者

text Chat application using thread or fork

开发者 https://www.devze.com 2023-04-12 13:05 出处:网络
I am facing a problem that i am making a text chat program. and i will run this same program in two different terminals at the same time. i am using file in my program that is at one terminal i will w

I am facing a problem that i am making a text chat program. and i will run this same program in two different terminals at the same time. i am using file in my program that is at one terminal i will write data it will be stored in the file and next terminal will read it and display similarly vice the versa.

i have write two functions send and receive now i want that both my send and receive will work at the same time that is when i am sending message at the same time i can receive message. what should i do i have tried forking but i guess i don't know ho to use it.and how should i manage this as same file is accessed by two process each access it two time any suggestion or help thanks this is my code up till now

#include<stdio.h>
#include <sys/stat.h>
#include<unistd.h>
void send()
  {
    char message[256];
    fgets(message , 256 , stdin);
    //printf("Message is : %s" , message);
    FILE * f1;
    f1= fopen("chatfile.txt", "w");
   if(f1== NULL)
   {
   printf("not open ");
   }
    fprintf(f1 , "%s" , message);
    fclose(f1);
  } 
  //-------------------------------------------------------
void recieve()
  {
    char message[256];
    FILE * f1;
开发者_运维知识库    f1= fopen("chatfile.txt", "r");
    fgets(message , 256 , f1);
    printf("Message is : %s" , message);
    fclose(f1);
   }
   //-------------------------------------------------------
int file_size()
  {
    struct stat st;
    stat("chatfile.txt" , &st);
    int size = st.st_size;
    return size;
  }
  //------------------------------------------------------
int main()
{
int size =0;

//printf("%d" , getpid());
pid_t pid;
pid = fork();
while(1)
{
if( pid == 0)

   {
   printf("parent");
   send();
    }
else 
  {
   printf("child");
   recieve();
  }
}     


}


The problem you have here is one of synchronisation. You have no idea when the send has been completed, the recieve could read partial results or none at all. You either need a mechanism such as a semaphore, or use a different medium such as a named pipe. You should also take into account your shutdown senario.

Here is a simplistic named pipe example, where I have retained as much as your code as possible:

#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>

/* Easier to alter if defined in one place 
   safere to put named-pipes in /tmp */
#define PIPENAME "/tmp/chatfile.pipe"

/* An empty parameter list means no parameter
   checking, not no parameters! */

void send(void)
{
    char message[256];

    fgets(message , 256 , stdin);

    FILE * f1;
    f1= fopen(PIPENAME, "w");

    if(f1 == NULL) { 
        /* printf writes to stdout
           perror writes to stderr, and includes the error */
        perror("not open ");
        exit(1);
    }

    fprintf(f1 , "%s" , message);
    fclose(f1);
}

//-------------------------------------------------------

void recieve(void)
{
    char message[256];
    FILE * f1;
    f1= fopen(PIPENAME, "r");

    /* You should check EVERY open */
    if (f1 == NULL) {
       perror("not open ");
       exit(1);
    }

    fgets(message , 256 , f1);
    printf("Message is : %s" , message);
    fclose(f1);
}

//------------------------------------------------------

int main(int argc, char *argv[])
{
    int iResult = mkfifo(PIPENAME,0666);
    if (iResult == -1 && errno != EEXIST) {
        perror("Unable to create pipe");
        exit(1);
    }

    pid_t pid;
    pid = fork();

    while(1)
    {
        if( pid == 0) {
            printf("parent");
            send();
        }
        else {
            printf("child");
            recieve();
        }
    }
    return 0;
}

I should also add that, with a named pipe, there is no need to keep closing and opening it again (although there is a limit to the number of bytes which are atomically written).

0

精彩评论

暂无评论...
验证码 换一张
取 消