Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Please help me,,,,,
subject:inter process communication using pipe
please give me a program in c to send N numbers from a parent process to child process and display them.
please please its urgent..
actually i am beginner in this IPC topic.
so please give me a consideration.
if possible give me the tutorial also.
/*
Compile: gcc -o fd_server fd_server.c
*/
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
int main(int argc, char *argv[])
{
int rdfd, wrfd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the first named - pipe */
remove(NP1);
ret_val = mkfifo(NP1, 0666);
if (ret_val == -1) {
perror("Error mkfifo(NP1)");
return 1;
}
else
printf("pipe %s created\n",NP1);
/* Create the second named - pipe */
remove(NP2);
ret_val = mkfifo(NP2, 0666);
if (ret_val == -1) {
perror("Error mkfifo(NP2)");
return 1;
}
else
printf("pipe %s created\n",NP2);
/* Open the first named pipe for reading */
if ((rdfd = open(NP1, O_RDONLY)) == -1) {
perror("Error open(NP1)");
return 1;
}
else
printf("NP1 opened in read mode FD-%d\n",rdfd);
/* Open the second named pipe for writing */
if ((wrfd = open(NP2, O_WRONLY)) == -1) {
perror("Error open(NP2)");
return 1;
}
else
printf("NP2 opened in write mode FD-%d\n",wrfd);
/* Read from the first pipe */
numread = read(rdfd, buf, MAX_BUF_SIZE);
buf[numread] = '\0';
printf("Full Duplex Server : Read From the pipe : %s\n", buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
/*
* Write the converted string back to the second
* pipe
*/
write(wrfd, buf, strlen(buf));
}
fd_client.c
Code:
/*
Compile: gcc -o fd_client fd_client.c
*/
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
int main(int argc, char *argv[])
{
int wrfd, rdfd, numread;
char rdbuf[MAX_BUF_SIZE];
/* Check if an argument was specified. */
if (argc != 2) {
printf("Usage : %s <string to be sent to the server>\n", argv[0]);
return 1;
}
/* Open the first named pipe for writing */
if ((wrfd = open(NP1, O_WRONLY)) == -1) {
perror("Error open(NP1)");
return 1;
}
else
printf("NP1 opened in write mode FD-%d\n",wrfd);
/* Open the second named pipe for reading */
if ((rdfd = open(NP2, O_RDONLY)) == -1) {
perror("Error open(NP2)");
return 1;
}
else
printf("NP2 opened in read mode FD-%d\n",rdfd);
/* Write to the pipe */
write(wrfd, argv[1], strlen(argv[1]));
/* Read from the pipe */
numread = read(rdfd, rdbuf, MAX_BUF_SIZE);
rdbuf[numread] = '\0';
printf("Full Duplex Client : Read From the Pipe : %s\n", rdbuf);
}
not able to find out the required materials from that....
Ohh and what were you expecting from Google ... "to give you a program in c to send N ... and your homework for you" ???
I asked you to search for the "named pipes" expecting you to understand how they work, try a few programs on your own and post back the problems you faced!!
...THANKS ANISHA......!!!
actuallllly, i was asked to do such a program by subject in charge.
and I am totally unaware about these pipes,message queue ,semaphore and all in the program side.(I mean, the implementation side...),...nyway thank you...
Teacher X sends two filenames to student Y who has to find out the file(s) which has
1.read permission
2.write permission
Implement using pipes.X and Y are processes.
...and, still I don't know how to solve this .....
if u have any materials to help such a beginning stage student please forward....
Perhaps you didn't understand what I was saying, here on this forum no one is going to write or give the whole code to you. No I am not being rude, but the volunteers of this forum can only guide and nudge you in the right direction for which you are supposed to show some efforts on your own.
again:
search Google with the keywords I wrote above,
find out some examples,
understand them,
modify them,
and then when you face problems, create a new thread in the programming section here using the code tags.
Last edited by Aquarius_Girl; 12-08-2010 at 03:22 AM.
Reason: typo
trying using man for associated c apis. man pages tent to have cone samples with the api explanations. They are not always well written documentations but They ship with almost any Linux system you can think of.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.