execl not running programming - c++

I have the following two simple programs:
bye.cc
#include <iostream>
int main()
{ std::cout << "Bye bye bye world" << std::endl; }
hello.cc
#include <cstdlib>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;
int main()
{
int status;
cout << "Hello world" << endl;
int pid = fork();
if (pid != 0) {
cout << "I am parent - " << pid << endl;
// wait for child to finish up......
cout << "Waiting for child to finish" << endl;
wait(&status);
cout << "Child finished, status " << status << endl;
} else {
cout << "--- I am child - " << pid << endl; // **Note**
execl("bye", "");
cout << "--- I am sleeping" << endl;
sleep(3);
exit(11);
}
}
In hello.cc, if the line marked "Note" is enabled (not commented), I get the expected behavior, sleep(3) is not executed, and "bye" is executed, expected msg printed to console.
$ ./hello
Hello world
I am parent - 27318
Waiting for child to finish
--- I am child - 0
Bye bye bye world
Child finished, status 0
However, when the line marked "Note" is commented, "bye" is NOT executed, and sleep(3) is performed.
$ ./hello
Hello world
I am parent - 27350
Waiting for child to finish
--- I am sleeping
Child finished, status 2816
Can someone please help me understand what might be going on. What I found very odd, if I replace the "cout" with a printf(), then the sleep performed.
Thank you,
Ahmed.

According to the spec, the argument list to execl must be terminated by a NULL pointer (i.e. (char *)0, not "").
Changing the nearby code is just changing what happens to be on the stack when you invoke execl. As written, the program's behavior is undefined.
P.S. Always check the return value of library routines for errors.

the exec family of functions, when successful, do not return.
That is why you do not see the sleep comment when the execl() is executed.

Related

fork() and waitpid() not waiting for child

I am having a bit of trouble getting waitpid to work could someone please explain what is wrong with this code?
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;
int main() {
string filename_memory;
decltype(fork()) pid;
if (!(pid = fork())) {
cout << "in child" << endl;
sleep(1);
}
else {
int status_child;
do {
waitpid(pid, &status_child, WNOHANG);
cout << "waiting for child to finish" << endl;
} while (!WIFEXITED(status_child));
cout << "child finished" << endl;
}
return 0;
}
If wait() or waitpid() returns because the status of a child process
is available, these functions shall return a value equal to the
process ID of the child process for which status is reported.
If waitpid() was invoked with WNOHANG set in options, it has at least
one child process specified by pid for which status is not available,
and status is not available for any process specified by pid, 0 is
returned. Otherwise, (pid_t)-1 shall be returned, and errno set to
indicate the error.
This means that the status_child variable has no meaning until waitpid returns the pid of the child.
You can fix this by applying these changes:
int ret;
do {
ret = waitpid(pid, &status_child, WNOHANG);
cout << "waiting for child to finish" << endl;
} while (ret != pid || !WIFEXITED(status_child));
cout << "child finished" << endl;

getppid() not returning parent's pid [duplicate]

This question already has answers here:
Why do processes I fork get systemd as their parent?
(3 answers)
Closed 6 years ago.
I have been trying to learn about fork and processes. I just encountered a small problem with this piece of code and was trying to understand why?.
I was trying to duplicate a process by a system call Fork and with the value of pid being positive, it hit the parent and its getpid() was returned. And simultaneously it hit the child and its getpid() was returned. But the problem was, when I called up the getppid() here, it was expected to show its parent's process identifier, which happened to be 3370.
But upon compilation and execution of this file, it showed the value of getppid() as 1517 (not parent's id).
I am using ubuntu 14.04 LTS on Oracle VM VirtualBox (32-bit O.S.). The code of this forking.cpp file is as follows:
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <cstdlib>
using namespace std;
int main()
{
pid_t pid1;
pid1 = fork();
if(pid1 == -1)
{
cout << "No child process formed: " << getpid() <<endl;
}
else if(pid1 == 0)
{
cout << "Child has been formed: " << getpid()<< " and its parent's id: " << getppid() << endl;
}
else if(pid1 > 0)
{
cout << "Parent process has been called: " << getpid() << endl;
}
cout << "END of Stuffs" << endl;
return 0;
exit(0);
}
For compilation, I was using the command g++ forking.cpp on terminal and for executing, ./a.out.
Then it showed this:
Parent process has been called: 3370
END of Stuffs
Child has been formed: 3371 and its parent's id: 1517
END of Stuffs
shashish-vm#shashishvm-VirtualBox:~/Desktop$
I know that trivially, if a parent dies before its child, the child is automatically adopted by the original "init" process, with PID 1. But here it is definitely not this case.
This situation occurs when the parent process terminates before the execution of getppid(). Use wait(NULL) at the end the parent to solve the problem.

Pipe is not working correctly in the parent-child process

As I have just started with these concepts I might be missing out a few elementary things. I was trying to link the parent and the child processes (created by fork() function) using pipe. In the parent process, I wanted to write in the pipe descriptor (af[1]) and after closing up the write end, I wanted to read from the read end of the pipe with descriptor (af[0]) in the child process.
Here is my code:
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <cstdlib>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
using namespace std;
int main()
{
pid_t pid1;
pid1 = fork();
int af[2],nbytes,wbytes;
pipe(af);
char inside[20];
if(pid1 == -1)
{
cout << "No child process formed: " << getpid() <<endl;
exit(1);
}
else if(pid1 == 0)
{ cout<< "inchild" <<endl;
close(af[1]);
nbytes = read(af[0],inside,strlen(inside));
cout << "read bytes: "<< nbytes << endl;
cout << "child(read) inside descriptor: " << inside << endl;
close(af[0]);
cout << "in child's end" << endl;
exit(0);
}
else
{ cout<< "inparent" << endl;
close(af[0]);
wbytes = write(af[1],"Hello World",12);
cout<< "wrote bytes: " << wbytes<<endl;
cout << "Parent(write) inside string: " << af[1] << endl;
close(af[1]);
cout << "in parent's end" << endl;
exit(0);
}
return 0;
}
Then I was expecting this to run as follows:
Goes into the parent -> write string,
Close write end,
Goes into the child -> read string into inside,
Show result of string (Hello World),
Close read end.
But what I was getting here is this result:
inparent
shashish-vm#shashishvm-VirtualBox:~/Desktop$ inchild
read bytes: 0
child(read) inside descriptor:
A��M�N��sf�
in child's end
And it was still not terminating.
I was using Ubuntu 14.04 LTS on Oracle VM VirtualBox (32-bit O.S.). And I have no idea why it was doing like this. I knew it is the job of the scheduler to switch the processes but still pipe functionality of IPC was not working there. The write process occurred even if I removed close(af[0]) statement but still the reading was not happening properly.
You problem is that you open the pipe after calling fork. This means the parent and child have different pipes. You can fix it by moving the call to pipe before the fork to create a single linked pipe.

Synchronization in Pthread C++

I've read about synchronized thread in Posix threads tutorial. They say that function pthread_join is used for waiting thread until it stops. But why doesn't this idea work in that case?
Here is my code:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[5];
void* thread(void *params)
{
cout << "Hello, thread!" << endl;
cout << "How are you, thread? " << endl;
cout << "I'm glad to see you, thread! " << endl;
}
void* thread2(void *params)
{
cout << "Hello, second thread!" << endl;
cout << "How are you, second thread? " << endl;
cout << "I'm glad to see you, second thread! " << endl;
// for (;;);
}
int main()
{
pthread_t pt1, pt2;
int iret = pthread_create(&pt1, NULL, thread, NULL);
int iret2 = pthread_create(&pt2, NULL, thread2, NULL);
cout << "Hello, world!" << endl;
pthread_join(pt1, NULL);
cout << "Hello, middle!" << endl;
pthread_join(pt2, NULL);
cout << "The END" << endl;
return 0;
}
Threads are executed asynchronously, as someone already mentioned in answer to question you linked. Thread execution starts right after you create() it. So, at this point:
int iret = pthread_create(&pt1, NULL, thread, NULL);
thread() is already executing in another thread, possibly on another core (but it doesn't really matter). If you add a for (;;); in your main() right after that, you will still see thread message being printed to console.
You also misunderstood what join() does. It waits for thread termination; as your threads don't do any real work, they will (most probably) reach their ends and terminate way before you call join() on them. Once again: join() doesn't start execution of thread in given place, but waits for it to terminate (or just returns, if it's already terminated).

Why am I getting an error when using vfork()?

This is my code... I don't know why I'm get an error segment... could somebody explain the reason to me?
#include <iostream>
#include <string>
// Required by for routine
#include <sys/types.h>
#include <unistd.h>
using namespace std;
int globalVariable = 2;
main()
{
string sIdentifier;
int iStackVariable = 20;
pid_t pID = vfork();
if (pID == 0) // child
{
// Code only executed by child process
sIdentifier = "Child Process: ";
globalVariable++;
iStackVariable++;
cout << "PROCESO NUMERO"<<getpid()<<sIdentifier;
// printf("Proceso hijo: PID %d - PPID %d\n", getpid(), getppid());
cout << " Global variable: " << globalVariable;
cout << " Stack variable: " << iStackVariable << endl;
return (0);
}
else if (pID < 0) // failed to fork
{
cerr << "Failed to fork" << endl;
return (1);
// Throw exception
}
else // parent
{
// Code only executed by parent process
sIdentifier = "Parent Process:";
}
// executed only by parent
cout << sIdentifier;
cout << " Global variable: " << globalVariable;
cout << " Stack variable: " << iStackVariable << endl;
return (0);
}
Is this of use ? Note the caveats surrounding modification of variables.
The vfork() function has the same effect as fork(), except that the behaviour is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions.
If you vfork() both processes are sharing an address space. You should probably only use vfork() if you are going to exec another process pretty much immediately in the child. The reason the system call was created was to avoid the overhead of copying every page in the parent process's address space only to have all those mappings discarded when the child exec's. For your case, use fork() instead.