I am getting a warning > Implicit declaration of function ‘wait’ < and when I run the program it works correctly, I would like to understand why I am getting this warning?
Thanks in advance
Edit: I forgot to add the library included
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void create (char* program, char** arg_list)
{
/* put your code here */
pid_t childPid;
int status;
if((childPid = fork()) < 0){
printf("Failed to fork() --- exiting...\n");
exit(1);
}
else if (childPid == 0){ // --- inside the child process
if(execvp(program, arg_list) < 0){ // Failed to run the command
printf("*** Failed to exec %s\n", program);
exit(1);
}
}
else{ // --- parent process
while(wait(&status) != childPid)
printf("...\n");
}
}
You are probably missing the headers for wait(2):
#include <sys/types.h>
#include <sys/wait.h>
You need to put:
#include <sys/types.h>
#include <sys/wait.h>
at the top of the program to get the declaration of the function.
This is shown in the man page
Related
I need create unnamed pipe in C without fork();
I have code with fork, but I can't find any information about unnamed pipe without fork. I read that this is an old solution, but it just needs it. Can anyone help me?
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define KOM "Message to parent\n"
int main()
{
int potok_fd[2], count, status;
char bufor[BUFSIZ];
pipe(potok_fd);
if (fork() == 0) {
write(potok_fd[1], KOM, strlen(KOM));
exit(0);
}
close(potok_fd[1]);
while ((count = read(potok_fd[0], bufor, BUFSIZ)) > 0)
write(1, bufor, count);
wait(&status);
return (status);
}
You should be more precise, what do you need it to do? Just sending a message to yourself within the same process doesn't make much sense.
Anyway you can literally just not fork and do everything inside one process, it's just not really useful.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#define KOM "Message to parent\n"
int main()
{
int potok_fd[2], count;
char bufor[BUFSIZ];
pipe(potok_fd);
write(potok_fd[1], KOM, strlen(KOM));
fcntl(potok_fd[0], F_SETFL, O_NONBLOCK);
while ((count = read(potok_fd[0], bufor, BUFSIZ)) > 0)
write(1, bufor, count);
return 0;
}
I'm trying to use date and wc with pipes on CentOS. I'm not able to printf that I'm in parent or child. Any help is appreciated.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
/* pipe1.c - send information through pipe. */
void syserr(char* msg)
{
printf("%s", msg);
}
void child(int pfd[]){
dup2(pfd[1],1);
execl("/bin/date", "date", 0);
}
void main()
{
int pfd[2], i, pid;
char str[] = "Hello World!\n";
if (pipe(pfd) == -1)
syserr("pipe");
printf("pfd[0] = %d, pfd[1] = %d\n", pfd[0], pfd[1]);
pid=fork();
switch(pid) {
case -1:
syserr("fork");
case 0:
{
printf("I'm child'");
child(pfd);
}
default:{ /* parent only */
if(pid!=0)
{
printf("I'm parent'");
dup2(pfd[0],0); //input
execl("/bin/wc", "wc", 0);
}/*default*/
} /*switch*/
}
}
Remember that <stdio.h> is buffered, and stdout is generally line-buffered, at least when it is a terminal. See setvbuf(3)
So you should either end each of your printf format control string with a \n or call fflush(3) at appropriate places. In particular, do a fflush(NULL); before your fork and your execl.
Also use perror on failure (i.e. replace every call to syserr by perror) to understand how system calls are failing. See perror(3) & errno(3) & strerror(3).
BTW, your main is incorrectly declared. You should enable all warnings and debug info when compiling (e.g. compile with gcc -Wall -Wextra -g). Improve your code to get no more warnings. Then use the debugger gdb ...
Notice that to avoid zombie processes, your parent process should use some waiting system call like waitpid(2) or wait(2) or wait4(2)
Works now. I had to add close(pfd[0]); in child, and close(pfd[1]); in parent.
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
/* pipe1.c - send information through pipe. */
void child(int pfd[]){
printf("I'm in child func\n");
close(pfd[0]);
dup2(pfd[1],1);
execl("/bin/date", "date", 0);
}
int main(){
int pfd[2], pid;
if (pipe(pfd) == -1) perror("pipe");
printf("pfd[0] = %d, pfd[1] = %d\n", pfd[0], pfd[1]);
fflush(NULL);
pid=fork();
switch(pid) {
case -1:
perror("fork");
case 0:
{
printf("I'm child\n");
child(pfd);
}
default:{ /* parent only */
if(pid!=0){
printf("I'm daddy\n");
close(pfd[1]);
dup2(pfd[0],0); //input
execl("/bin/wc", "wc", 0);
}/*default*/
} /*switch*/
}
return 0;
}
I am learning C and have run into a small problem. After reading about fork() bomb on Wikipedia and on StackOverflow. I wanted to implement the same, but using command line args.
I want to endlessly call firefox/chrome, but unable to do the same in my below program. Any assistance would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
int main(int argc, char **argv)
{
pid_t pid;
char *parmList[] = {"firefox", "index.html", NULL};
int a;
if ((pid = fork()) == -1)
{
perror("fork failed");
}
if (pid == 0)
{
a = execvp("/usr/bin/firefox", parmList);
fprintf(stdout, "execvp() returned %d\n", a);
fprintf(stdout, "errno: %s (%d).\n", strerror(errno), errno);
}
else
{
waitpid(pid, 0, 0);
}
return 0;
}
You should clarify what error you're getting, since I don't want to run a fork bomb, but the code you wrote doesn't bomb (call fork() within a loop). It spawns one process, waits nicely, and quits.
I'm trying to use the function execlp() to make a child processes run a specific code that I wrote, but I don't understand how the path works.
NOTE: I use export PATH=$PATH:. in the terminal so I don't need to type /.ProgName every time.
The first program is:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
main() {
pid_t childpid;
int i;
int nprocess = 3;
for (i = 0; i < nprocess; ++i) {
if ((childpid = fork()) < 0) {
perror("fork:");
exit(1);
}
if (childpid==0){
printf("I'm the son with ID= %ld\n",(long)getpid());
char *path = "exectest";
if ((execl(path,"0",NULL))<0) printf("\n error exec \n");
}
else
printf("I'm the father with ID= %ld",(long)getpid());
}
}
The second program to be called by the execlp is :
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
void main(){
printf("\n I'm using exec \n");
}
Both programs are in the same directory. The second program is named "exectest" but when I launch the first, I get the error message.
You should compile your second file exectest.c and get executable file which called exectest, because of your first program launch executable file exectest.
I'd really love your help with understanding why doesn't the process reach the "son process" after using fork() command. I'm trying to write a program that runs another program, but It seems that the program dosen't even reach the son process. I can tell that since "son process" is not being printed to the screen, and I really wonder why.
Here's a sketch of the code- I can't even check if it is alright since as I said, it doesn't even reaching the son process, I always get "son exited with error".
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <time.h>
#define MAXARGV 5;
int main() {
char* cmd;
int child_status;
char* s;
char** argv;
int counter;
cmd= (char*) calloc( 5, sizeof(char)*20);
s=(char*) calloc(1,sizeof(char)*20);
argv=(char**) calloc(5, sizeof(char*)*20);
printf("Please write a command\n");
gets(cmd);
counter = 0;
while (strcmp(cmd, "exit") != 0) {
int pid = fork();
if (pid == 0) {
printf("son process");
while (sscanf(cmd, "%s", s) == 1) {
strcpy(argv[counter], s);
counter++;
}
execv(argv[0], argv);
printf("the command is not legal");
assert(0);
}
else {
if (wait(&child_status) == -1) {
printf("error waiting for pid=%d\n", pid);
exit(-1);
}
if(WIFEXITED(child_status)!=0)
printf("son status=%d\n", WEXITSTATUS(child_status));
else
printf("son exited with error\n");
}
printf("Please write a command");
gets(cmd);
}
free(s);
free(cmd);
free(argv);
printf("here as well");
return 1;
}
The program reaches the printf("son process") just fine, but that just puts the string in a buffer inside the process and since you didn't fflush() it, it doesn't make it to the screen and is discarded with the rest of the process' memory in the exec call. Note, that stdout is normally line-buffered, so if you had newline there, it would auto-flush. Also stderr is by default unbuffered and more suitable for debug prints (fprintf(stderr, "child process")).
You are trying to assemble the command read from standard input in argv, but it only has memory for the actual arguments given to you, so you overrun this memory and get segmentation fault.
if WIFEXITED gives zero, you should use WIFSIGNALED and WTERMSIG to confirm that the error is indeed SIGSEGV.
assert(0) is not a good way to terminate process after error. exit(1) is. Assertions are only for conditions that indicate bug in the code itself if they happen and are often eliminated (by defining NDEBUG) from production code.