Child returned status 2 linux

Linux kernel forked child return of status

Solution 1: Process exit status is limited in value, therefore it is not the best way to communicate a value between child and parent. : Question: I have this line in my code : When the child process works, the value of status is 0, well.

Linux kernel forked child return of status

So here’s my dilemma: Why when I fork(); a child of a process and it ends, the returned &status of the child is shifted to the left by 8 bit?

For example let’s say i have exit(4); at the end of the forked child , my status in the wait(&status); of the father process gets me 0x400 .

so here is some code that illustrates what I mean

#include main() < int n, status, cpid; printf("Parent pid = %d\n", getpid()); n = fork(); if (n != 0) < // parent code printf ("I'm the parent with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid()); status = 0; sleep(5); // verify status of child cpid = wait(&status); // so when i printf the hex value of status it gets shifted printf("I received from my child %d this information %x\n", cpid, status); >else < // child code printf ("I'm the child with PID %d \t fork returned %d \t my parent is %d\n", getpid(), n, getppid()); sleep(20); printf("Child complete\n"); status=12345; // the line that returns the shifted value exit(4); >printf("Parent complete\n"); exit(15); > 

Read the documentation on wait(3) . The value returned is a 32-bit integer containing the exit status (if the process exit ed normally), as well as a number of flag bits. To determine if the process exited normally, use the WIFEXITED() macro. If that returns true, then use the WEXITSTATUS() macro to get the actual exit status:

int status; if(wait(&status) > 0) < // Did the process exit normally? if(WIFEXITED(status)) < // This is the value you really want int actual_status = WEXITSTATUS(status); . >> 

Because status hold not only the return code of the child bu it holds another information.
if you want to know the exit value you have to use the WEXITSTATUS macro

WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should only be employed if WIFEXITED returned true.

that’s mean that the status integer holds another information and to extract this information you should use the macros which defined in sys/wait.h

Exit status of a child process in Linux, It is known that fork() system call is used to create a new process which becomes child of the caller process. Upon exit, the child leaves an exit …

How to get return value from child process to parent?

I’m supposed to return the sum of first 12 terms of Fibonacci series from child process to parent one but instead having 377 , parent gets 30976 .

#include #include #include #include #include int main(int argc, char *argv[]) < pid_t childpid; int i, fib_sum=0, fib1=1, fib2=1, temp, status; childpid=fork(); if(childpid!=0) < wait(&status); fprintf(stderr, "%d\n", status); >else < for(i=1; ifprintf(stderr, "%d\n", fib_sum); return fib_sum; > > 

I’m supposed to return the sum of first 12 terms of Fibonacci series from child process to parent one but instead having 377, parent gets 30976.

Process exit status is limited in value, therefore it is not the best way to communicate a value between child and parent.

Читайте также:  Рабочая станция заблокирована линукс

One of the solution is to pass the calculated value using pipes.

#include #include #include #include #include int main(int argc, char *argv[]) < pid_t childpid; int i, fib_sum=0, fib1=1, fib2=1, temp, status; int fd[2]; int val = 0; // create pipe descriptors pipe(fd); childpid = fork(); if(childpid != 0) // parent < close(fd[1]); // read the data (blocking operation) read(fd[0], &val, sizeof(val)); printf("Parent received value: %d\n", val); // close the read-descriptor close(fd[0]); >else // child < // writing only, no need for read-descriptor: close(fd[0]); for(i=1; i// send the value on the write-descriptor: write(fd[1], &fib_sum, sizeof(fib_sum)); printf("Child send value: %d\n", fib_sum); // close the write descriptor: close(fd[1]); return fib_sum; > > 
Child send value: 377 Parent received value: 377 

If you can’t use pipes, which would be the optimal solution here, you could save the result to a file that the parent would read from. Pass the name of the file to save the result to from parent to child. In your child process, you would do:

int main(int argc, char *argv[]) < int fib_sum=0; if (argc //. calculate fib_sum FILE *f = fopen(argv[1], "w"); if (f == NULL) < printf("Error opening file!\n"); return 1; >fprintf(f, "%d", fib_sum); return 0; > 

Then in your parent process:

int n = 0; FILE* f; //. spawn child and wait FILE *f = fopen(file_name, "r"); fscanf(f, "%d", &n); 

Bzip2 — Check tar file for errors, gzip: stdin: not in gzip format. This tells you the file is not compressed with gzip. Therefore, tar ‘s -z option should not be used. If you want …

I used wait(&status) and the value of status is 256, why?

I have this line in my code :

When the child process works, the value o f status is 0, well.

But why does it return 256 when it doesn’t work? And why changing the value of the argument given to exit in the child process when there is an error doesn’t change anything (exit(2) instead of exit(1) for example)?

Edit : I’m on linux, and I compiled with GCC.

I defined status like this

int main(int argc, char **argv) < pid_t pid; int res; pid = fork(); if (pid == 0) < printf("child\n"); exit(1); >pid = wait(&res); printf("raw res=%d\n", res); return 0; > 

. the value of res will be 256 . This is because the return value from wait encodes both the exit status of the process as well as the reason the process exited. In general, you should not attempt to interpret non-zero return values from wait directly; you should use of the WIF. macros. For example, to see if a process exited normally:

 WIFEXITED(status) True if the process terminated normally by a call to _exit(2) or exit(3). 

And then to get the exit status:

 WEXITSTATUS(status) If WIFEXITED(status) is true, evaluates to the low-order 8 bits of the argument passed to _exit(2) or exit(3) by the child. 
int main(int argc, char **argv) < pid_t pid; int res; pid = fork(); if (pid == 0) < printf("child\n"); exit(1); >pid = wait(&res); printf("raw res=%d\n", res); if (WIFEXITED(res)) printf("exit status = %d\n", WEXITSTATUS(res)); return 0; > 

You can read more details in the wait(2) man page.

The status code contains various information about how the child process exited. Macros are provided to get information from the status code.

If status is not NULL, wait() and waitpid() store status information in the int to which it points. This integer can be inspected with the following macros (which take the integer itself as an argu- ment, not a pointer to it, as is done in wait() and waitpid()!): WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit(3) or _exit(2), or by returning from main(). WEXITSTATUS(status) returns the exit status of the child. This consists of the least significant 8 bits of the status argument that the child specified in a call to exit(3) or _exit(2) or as the argument for a return statement in main(). This macro should be employed only if WIFEXITED returned true. WIFSIGNALED(status) returns true if the child process was terminated by a signal. WTERMSIG(status) returns the number of the signal that caused the child process to terminate. This macro should be employed only if WIFSIGNALED returned true. WCOREDUMP(status) returns true if the child produced a core dump. This macro should be employed only if WIFSIGNALED returned true. This macro is not specified in POSIX.1-2001 and is not available on some UNIX implementations (e.g., AIX, SunOS). Only use this enclosed in #ifdef WCOREDUMP . #endif. WIFSTOPPED(status) returns true if the child process was stopped by delivery of a signal; this is possible only if the call was done using WUNTRACED or when the child is being traced (see ptrace(2)). WSTOPSIG(status) returns the number of the signal which caused the child to stop. This macro should be employed only if WIFSTOPPED returned true. WIFCONTINUED(status) (since Linux 2.6.10) returns true if the child process was resumed by delivery of SIGCONT. 

SUGGESTION: try one of the following «Process Completion Status» macros:

 int status = 0; .. int retval = wait (&status); if (WIFEXITED(status)) printf("OK: Child exited with exit status %d.\n", WEXITSTATUS(status)); else printf("ERROR: Child has not terminated correctly.\n"); 

Gzip — Windows command line tar «cannot connect to d, Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. …

Читайте также:  Linux смена сетевой карты

Источник

Thread: «Child returned status 2», what is this?

dderolph is offline5 Cups of Ubuntu

«Child returned status 2», what is this?

I’m running Ubuntu 10.04LTS as a virtual machine using VMware Player. I want to use Terminal to use tar to install VMware Tools for VMware Player per instructions at http://www.vmware.com/support/ws55/d. html#wp1127214. I’m getting the following error message. Can someone shed some light on this?

david@ubuntu:~$ sudo tar zxpf /mnt/cdrom/VMwareTools-8.4.5-324285.tar.gz
[sudo] password for david:
tar: /mnt/cdrom/VMwareTools-8.4.5-324285.tar.gz: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Child returned status 2
tar: Exiting with failure status due to previous errors
david@ubuntu:~$

coolbeans777 is offline5 Cups of Ubuntu

Re: «Child returned status 2», what is this?

You are in your home directory, try using cd to go to the directory where the tar.gz file is downloaded. If you dont understand how to use cd google it.

Habeouscorpus is offlineFrothy Coffee!

Join Date Nov 2010 Location In front of my computer? Beans 209 —> Beans 209 Distro Ubuntu 11.04 Natty Narwhal

Re: «Child returned status 2», what is this?

That usually means that there was a fatal error. Something went wrong. In this case, the file did not exist, and the program quit. Check for typos.

Imitation is suicide. To be great is to be misunderstood.

Источник

tar shows «is not a bzip2 file» error when uncompressing a .tar.bz file

maybe it was wrongly named? if you feel lucky try tar xzvf . or tar xvf . — otherwise maybe a look into the head may reveal details.

Try file data_or.tar.bz which should tell you if the file is simply a mis-named archive of a different type.

2 Answers 2

Your tarball is uncompressed. The extension .bz is obsolete and misleading.

You can decompress using the following command:

Читайте также:  Перезапуск графической оболочки linux

What probably happened here is that data_or.tar.bz was created with the —auto-compress switch (or tar -cavf ) that chooses the compression algorithm from the supplied extension.

The proper extension for bzip2 compressed files is .bz2 , while the .bz extension is for bzip compressed files.

bzip uses arithmetic encoding (which is a patented algorithm), so bzip2 was created in 1997 as a patent-free alternative. As a result, bzip2 and bzip are incompatible.

tar cannot handle bzip (de)compression, so the —auto-compress switch resulted in an uncompressed tarball.

tar -xvjf file.tar.bz2 >> Because the flag ‘j’ is used, tar command will call bzip2 to process the decompression.

bzip2: (stdin) is not a bzip2 file. >> bzip2 reported to parent process (tar) that the received file to process is NOT bz2 format

tar: Child returned status 2 >> tar reports the error status received from bzip2 child process by exiting execution. In man documentation, error status means «2 to indicate a corrupt compressed file». As bzip2 did not recognized the format, it reports it as ‘corrupt’

tar: Error is not recoverable: exiting now >> According to message received from child, compressed file is not accepted for processing

Steps to fixing the issue:

1- verify the format of the compression

POSIX tar archive (GNU) — «Or whatever other format», then correct it with the next step 2

2- tar -xvf file.tar.bz2 — «in case its a posix tar archive format»

Источник

tar: Error is not recoverable: exiting now

I would try to unzip and untar separately and see what happens:

mv Doctrine-1.2.0.tgz Doctrine-1.2.0.tar.gz gunzip Doctrine-1.2.0.tar.gz tar xf Doctrine-1.2.0.tar 

I encountered this error today when trying to unzip and untar mediawiki-1.18.0.tar.gz . I re-downloaded mediawiki-1.18.0.tar.gz and the errors stopped, so I assume the file must have been corrupted during the initial download.

It’s possible your tar file is not zipped. I just had this same error, but all I had was a plain old tar file. So try just removing the z from your flags. The z flag unzips your tar file as well as whatever other commands you requested with other flags. i.e. try:

Notice I removed the z from -xvzf

If you got «Error is not recoverable: exiting now» You might have specified incorrect path references.

[me@host ~]$ tar -xvf nameOfMyTar.tar -C /someSubDirectory/ tar: /someSubDirectory: Cannot open: No such file or directory tar: Error is not recoverable: exiting now [me@host ~]$ 

Make sure you provide correct relative or absolute directory references e.g.:

[me@host ~]$ tar -xvf ./nameOfMyTar.tar -C ./someSubDirectory/ ./foo/ ./bar/ [me@host ~]$ 

Right, I don’t answer the users specific question, but the error message in the title is the same as mine.

Try to get your archive using wget , I had the same issue when I was downloading archive through browser. Than I just copy archive link and in terminal use the command:

The problem is that you do not have bzip2 installed. The tar program relies upon this external program to do compression. For installing bzip2, it depends on the system you are using. For example, with Ubuntu that would be on Ubuntu

sudo apt-get install bzip2 

The GNU tar program does not know how to compress an existing file such as user-logs.tar (bzip2 does that). The tar program can use external compression programs gzip, bzip2, xz by opening a pipe to those programs, sending a tar archive via the pipe to the compression utility, which compresses the data which it reads from tar and writes the result to the filename which the tar program specifies.

Alternatively, the tar and compression utility could be the same program. BSD tar does its compression using lib archive (they’re not really distinct except in name).

Источник

Оцените статью
Adblock
detector