Thread: Simultaneous processes

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    Simultaneous processes

    I have done a small amount of programming using fork() to create a child process. I want to have a client/server pair to be able to send message string back and forth simulataneously so that while one is typing in a message, a message can be receieved from the other and printed. I supposed I could use fork but is there a better way to do this?

    To make it a little more clear:

    Say a user is typing in a message, at the same time a message comes over the line. I want to be able to print that message to the screen WHILE the user is still typing in a message.

    Any help is appreciated.

    Thanks
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You should explore select() and see if that does what you need.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    I'll try it.

    I've done some minor code using select, so I am not sure if it will read the socket while a user is inputting a string to send.

    Thanks
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I am pretty sure on *nix you can mix and match sockets and file descriptors.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    select?? that's for sockets... bleh

    Use a fifo !
    something simple like
    Code:
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    
    #define FIFO_NAME "/tmp/tha_fifo_stuff"
    
    int main(){
    	int cpid, fd;
    	char msg[1000] = "";
    	mkfifo(FIFO_NAME,S_IRWXU);
    
    	switch(cpid = fork()){
    	case -1:perror("fork()");
    		return 0;
    	case 0: //child
    		fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK);
    		strcpy(msg,"hello daddy");
    		write(fd,msg,strlen(msg)+1);
    		close(fd);
    		return 0;
    	default:
    		wait(&cpid);
    		fd = open(FIFO_NAME,O_RDONLY);
    		read(fd,msg,999);
    		printf("my son with pid %d sayd: '%s'\n",cpid,msg);
    		close(fd);
    	}
    	unlink(FIFO_NAME);
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Quote Originally Posted by xErath
    select?? that's for sockets... bleh

    Use a fifo !
    something simple like
    Code:
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    
    #define FIFO_NAME "/tmp/tha_fifo_stuff"
    
    int main(){
    	int cpid, fd;
    	char msg[1000] = "";
    	mkfifo(FIFO_NAME,S_IRWXU);
    
    	switch(cpid = fork()){
    	case -1:perror("fork()");
    		return 0;
    	case 0: //child
    		fd = open(FIFO_NAME,O_WRONLY|O_NONBLOCK);
    		strcpy(msg,"hello daddy");
    		write(fd,msg,strlen(msg)+1);
    		close(fd);
    		return 0;
    	default:
    		wait(&cpid);
    		fd = open(FIFO_NAME,O_RDONLY);
    		read(fd,msg,999);
    		printf("my son with pid %d sayd: '%s'\n",cpid,msg);
    		close(fd);
    	}
    	unlink(FIFO_NAME);
    	return 0;
    }

    It didnt work for me. Parent kept waiting. Never received the message from "son". Here is the one that worked.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    #define FIFO_NAME "/tmp/tha_fifo_stuff"
    
    int main(){
    	int cpid, fd;
    	char msg[1000] = "";
    	pid_t chpid;
    
    	mkfifo(FIFO_NAME,S_IRWXU);
    
    	switch(cpid = fork()){
    	case -1:perror("fork()");
    		return 0;
    	case 0: //child
    		fd = open(FIFO_NAME,O_WRONLY);
    		if (fd<0)
    		{
    			perror("open");
    			exit(0);
    		}
    		strcpy(msg,"hello daddy");
    		write(fd,msg,strlen(msg)+1);
    		sleep(5);
    		close(fd);
    		exit(0);
    	default:
    		fd = open(FIFO_NAME,O_RDONLY);
    		read(fd,msg,999);
    		printf("my child with pid %d sayd: '%s'\n",cpid,msg);
    		wait(&chpid);
    		printf("my child has exited. My wait is over. bye!!!\n");
    		close(fd);
    	}
    	unlink(FIFO_NAME);
    	return 0;
    }
    My output.

    [root@linux-outside snoopy]# ./fifo1
    my child with pid 13124 sayd: 'hello daddy'
    my child has exited. My wait is over. bye!!!
    [root@linux-outside snoopy]#

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  3. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  4. Computer Processes.... Which can be stopped?
    By Sevrin in forum Tech Board
    Replies: 3
    Last Post: 06-08-2003, 08:13 PM
  5. Running simultaneous processes
    By marksw in forum Windows Programming
    Replies: 5
    Last Post: 05-19-2003, 05:07 AM