GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Mar-2005, 01:55
janhavideshpand janhavideshpand is offline
New Member
 
Join Date: Mar 2005
Posts: 2
janhavideshpand is on a distinguished road

Communication between processes in C under Minix


Hi All:
I'm trying to do my program on MINIX operating system,but program is in c .program that creates a mixture of compute-bound and I/O-bound processes. The numbers of each type of process should be supplied as command-line arguments. Each child process should periodically send a message (using a pipe) to the parent process, identifying itself, along with the current wall-clock time. The parent will log these messages to a text file. This text file should be created with a call to open() (i.e., don't use stdout). I/O-bound processes should repeatedly write and/or read one or more files. Compute-bound processes should sit in a tight loop, or do some other CPU-intensive operations before each message to the parent. (Extra Credit: write the child process as a separate program that outputs to stdout, and invoke it with an execl(). Use close() and dup() to setup the pipe as stdout before the execl().)

i created 2 processes,connected via pipe,can write msg into buf,but getting problem with opening file,i dont know how to do that,as with time.
please help me....
Thank you
Jan
  #2  
Old 13-Mar-2005, 08:14
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Jan and welcome to GIDForums™.

It appears that your assignment wants you to use the low level file operators of open, read, write and close.

If you could describe what problems that you are having useing open, I can try to help you more, but the basis of open is to pass it a filename and a flag. It will return an open file descriptor (int) upon success.

CPP / C++ / C Code:
int open(const char* pathname, int flags);

You probably would want to use the O_CREAT flag which will create the file if it does not exist or open if it does exist. You may also want to use O_APPEND if you want to write to the end of the file.

Then you will probably want to use write:

CPP / C++ / C Code:
 ssize_t write(int fd , const void * buf , size_t count );

to write a string, you could do something like:

CPP / C++ / C Code:
write(fd, (void*) string, strlen(string));

As this is low-level, it will not put a new line or any other seperator for you. You need to do that yourself.

Not sure if that helps, because I don't know where you are struggling...
  #3  
Old 13-Mar-2005, 14:53
janhavideshpand janhavideshpand is offline
New Member
 
Join Date: Mar 2005
Posts: 2
janhavideshpand is on a distinguished road

thak for reply


As i said in prev mail,i have created parent and child processes,child is writing mesg to buffer via pipe,and parent is reading msg from child,but i want to open a file for parent to read those msgs from child.
Thanks
Jan
  #4  
Old 13-Mar-2005, 20:13
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Hi Janhavi,

Following is the sample program to exchange messages between 2 process and write the messages received in parent in a log file.

Not sure if this is exactly what you are looking for, but will definitely give you a headstart.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

    int main()
    {
        int pfds[2];
		char buffer[256];
		int rd,wr,file_exist;
		int dst_handle;

        if (pipe(pfds)==-1)
        {
			perror("pipe");
			exit(0);
        }
		
		if ((dst_handle=open("msg.txt",O_WRONLY))!=-1)
		{
			/* File already exists */
			file_exist=1;
		} else {
			 if ((dst_handle=creat("msg.txt",00666))==-1)
			 {
				 perror("File create");
				 exit(1);
			 } 
			 if ((dst_handle=open("msg.txt",O_WRONLY))==-1)
			 {
				 perror("File open write");
				 exit(1);
			 }
		}


        if (!fork()) 
		{
            close(1);       /* close normal stdout */
            dup(pfds[1]);   /* make stdout same as pfds[1] */
            close(pfds[0]); /* we don't need this */
            fprintf(stdout,"Hello There\n"); //Send message to parent.
			exit(0); //exit from child process
        } else {
            close(0);       /* close normal stdin */
            dup(pfds[0]);   /* make stdin same as pfds[0] */
            close(pfds[1]); /* we don't need this */
			while ((rd=read(pfds[0],buffer,256))!=0)
			{
				if (rd==-1)
				{
					printf("\nError reading from pipe\n");
					exit(1);
				}
				wr=write(dst_handle,buffer,rd*(sizeof(char)));
				if (wr==-1)
				{
					printf("\nError writing to Log file\n");
					exit(1);
				}
			}
			            
        }
    }

 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent site review dr_wolf Websites Reviewed Forum 5 01-Feb-2005 19:15
urgent help needed :c + mysql insert jack C Programming Language 1 13-Apr-2004 22:16
urgent help:hash function jack C Programming Language 5 05-Apr-2004 13:18
urgent: problem + output filters jack Apache Web Server Forum 0 04-Feb-2004 23:32
urgent needs :apache + random function jack Apache Web Server Forum 0 19-Jan-2004 18:41

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:48.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.