ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hi friends,
this is a code written by me, here based on the value of "i" the corresponding case is executed and it lists the file in the specified directory... This is fine..
But how can i achive that all the 3 cases must be executed ( need not be using switch case ) that is i want all the 3 listings in the specified directories..( HOW CAN I DO THIS USING FORK() AND EXEC()and is there any other method to spawn all the 3 processess(i.e listings))
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int spawn3(void)
{
pid_t child_pid1,child_pid2,child_pid3;
int i=2;
char* arg_list1[] =
{
"ls", /* argv[0], the name of the program. */
"-l",
"/home/hello/include",
NULL /* The argument list must end with a NULL. */
};
char* arg_list2[] =
{
"ls", // argv[0], the name of the program.
"-l",
"/home/hello/scripting",
NULL // The argument list must end with a NULL.
};
char* arg_list3[] =
{
"ls", // argv[0], the name of the program.
"-l",
"/home/hello/Proj_conf",
NULL // The argument list must end with a NULL.
};
switch(i)
{
case 3: child_pid3=fork();
if (child_pid3 != 0)
return child_pid3;
else
{
execvp ("ls", arg_list3);
fprintf (stderr, "\nan error occurred in execvp\n\n\n");
}
break;
case 2: child_pid2=fork();
if (child_pid2 != 0)
return child_pid2;
else
{
execvp ("ls", arg_list2);
fprintf (stderr, "\nan error occurred in execvp\n\n\n");
}
break;
case 1: child_pid1=fork();
if (child_pid1 != 0)
return child_pid1;
else
{
execvp ("ls", arg_list1);
fprintf (stderr, "\nan error occurred in execvp\n\n\n");
}
break;
default: exit(0);
}
}
int main (int argc , char *argv[])
{
int child_pid;
child_pid=spawn3();
printf("returned to main() and the child pid is %d\n\n",child_pid);
return 0;
}
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Here's an example from Stephen G. Kochan & Patrick H. Wood, Topics in C Programming, Revised Edition (New York: John Wiley & Sons, Inc., 1991) that will give you an idea to build on. The program is a simple command-line interpreter (a "shell") and uses a clever bit of parsing (the breakup() function) to get arguments in the right places (in the argc[] array) before calling execvp().
Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <sys/types.h>
#include <sys/stat.h>
/*
* simple command interpreter
* supports < and > redirection and command line arguments
*/
int breakup (char *, char **);
void main (void)
{
char line [BUFSIZ], *args [15];
int process, nargs;
process = 0;
for ( ; ; ) { /* loop forever */
(void) fprintf (stderr, "cmd: ");
if (gets (line) == (char *) NULL) {
exit (EXIT_SUCCESS);
}
if ((process = fork ()) > 0) /* parent */
(void) wait ((int *) NULL);
else if (process == 0) { /* child */
/* parse command line */
nargs = breakup (line, args);
/* make sure there's something */
if (nargs == 0)
exit (EXIT_SUCCESS);
/* execute program */
(void) execvp (args [0], args);
/* some problem if execvp returns */
(void) fprintf (stderr, "cannot execute %s\n", line);
exit (errno);
} else if (process == -1) { /* can't create */
(void) fprintf (stderr, "can't fork\n");
exit (errno);
}
}
}
/*
* break up command line and return in "args"
* recognize < file and > file constructs and redirect
* standard input and output as appropriate
*/
int breakup (char *line, char *args [])
{
char *strptr = line, *file;
int nargs = 0;
while ((args [nargs] = strtok (strptr, " \t")) != (char *) NULL) {
strptr = (char *) NULL;
/* output redirection */
if (args [nargs] [0] == '>') {
if (args [nargs][1] != '\0')
file = &args [nargs] [1];
else {
file = strtok (strptr, " \t");
if (file == (char *) NULL) {
(void) fprintf (stderr, "no file after >\n");
return (0);
}
}
(void) close (1);
if (open (file, O_WRONLY | O_TRUNC | O_CREAT, 0666) == -1) {
(void) fprintf (stderr, "can't open %s for output\n", file);
return (0);
}
--nargs;
/* input redirection */
} else if (args [nargs] [0] == '<') {
if (args [nargs] [1] != '\0')
file = &args [nargs] [1];
else {
file = strtok (strptr, " \t");
if (file == (char *) NULL) {
(void) fprintf (stderr, "no file after <\n");
return (0);
}
}
(void) close (0);
if (open (file, O_RDONLY) == -1) {
(void) fprintf (stderr, "can't open %s for input\n", file);
return (0);
}
--nargs;
}
++nargs;
}
args [nargs] = (char *) NULL;
return (nargs);
}
As an aside, Kochan and Wood's books are excellent, full of working examples, and a great help to both beginners and those of us long in the tooth -- I understand there is a newly revised edition of Topics either just or just about to be published; might be worth a look-see.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.