pzozulka
asked on
C Programming: Execl
I have a C program on the server that uses a socket to "accept" connections. However, instead of communicating back with the client via that same socket, the professor asked for slightly different requirements.
He asked to fork & execl a program to do some processing, and use that program to talk back to the client.
After forking, inside the child process, I had to override my STDIN_FILENO & STDOUT_FILENO with the ClientFD (socket), so that the execl'ed program can talk to the client via the socket. This was required (for fork'ed program to use STDOUT to talk to client).
He asked to fork & execl a program to do some processing, and use that program to talk back to the client.
After forking, inside the child process, I had to override my STDIN_FILENO & STDOUT_FILENO with the ClientFD (socket), so that the execl'ed program can talk to the client via the socket. This was required (for fork'ed program to use STDOUT to talk to client).
csockfd = accept(sockfd, peerSockAddr, &peer_addr_size);
...
// Create Child Process
pid = fork();
...
if( pid == 0) {
// duplicate client fd to stdin and stdout
dup2(csockfd, STDIN_FILENO);
dup2(csockfd, STDOUT_FILENO);
close(sockfd);
execl("process","process");
}
The problem is that I cannot "PRINTF()" to the console. I need to be able to output some text to the console. What's the best way to do that?
ASKER
Is this the correct way to call execl?
execl("twist","twist","www.twitter.com"," 1","9000", "z");
If I was the user running this, I would type:
./twist www.twitter.com 1 9000 z
execl("twist","twist","www.twitter.com","
If I was the user running this, I would type:
./twist www.twitter.com 1 9000 z
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
fprintf(stderr, "Hello World!\n"); // instead of 'printf()'
It gives me an error: Input/output errorTwist.c is the client program which connects to deflect.c (acting as server) using a socket. Then deflect.c forks and execl process.c, and then process.c execl twist.c. Should I have fork'd before execl twist.c?
//Inside of deflect
csockfd = accept(sockfd, peerSockAddr, &peer_addr_size);
...
if( pid == 0) {
...
dup2(csockfd, STDIN_FILENO);
dup2(csockfd, STDOUT_FILENO);
close(sockfd);
execl("process","process");
...
}
// Inside of process.c ----- (execl'ed by deflect.c)
void process( int csockfd, const char *conf) {
FILE *fp=NULL;
// Open a log file in write mode.
fp = fopen ("/home/users1/pz951772/log.txt", "w+");
fprintf(fp, "Logging info...\n");
fflush(fp);
...
execl("twist","twist","www.twitter.com","1","9000","z", (char *)NULL);
exit(0);
}
int main( int argc, char *argv[] ) {
process(STDIN_FILENO, "config.txt");
}
// Inside twist.c
int main( int argc, char *argv[] ) {
...
if(argc > 4) {
fprintf(stderr, "Hello World\n"); // getting Input/output error
}
...
}
That's weird, this should work - 'stderr' is he only standard descriptor that you haven't bound to something else. Which user(s) are your processes working under?
Open in new window