Solved

C Programming: Execl

Posted on 2013-11-20
5
403 Views
Last Modified: 2013-11-20
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).

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");
}

Open in new window

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?
0
Comment
Question by:pzozulka
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39663831
You could write to 'stderr' instead of 'stdout', since the latter is no longer available,e.g.

fprintf(stderr, "Hello World!\n"); // instead of 'printf()'

Open in new window

0
 
LVL 8

Author Comment

by:pzozulka
ID: 39664274
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
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39664303
Not quite. The argument list needs to be NULL-terminated, i.e.

execl("twist","twist","www.twitter.com","1","9000","z",(char*)NULL);

Open in new window


See http://linux.die.net/man/3/execl

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39664603
fprintf(stderr, "Hello World!\n"); // instead of 'printf()'

Open in new window

It gives me an error: Input/output error

Twist.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");
...
}

Open in new window


// 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");	
}

Open in new window


// Inside twist.c
int main( int argc, char *argv[] ) {
   ...
   if(argc > 4) {
		fprintf(stderr, "Hello World\n"); // getting Input/output error
   }
   ...
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39664750
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?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to show menu 5 87
sameEnds challenge 3 133
Problem to save 10 147
Line meaning 9 81
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now