Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return code when OS kills your process

I've wanted to test if with multiply processes I'm able to use more than 4GB of ram on 32bit O.S (mine: Ubuntu with 1GB ram).

So I've written a small program that mallocs slightly less then 1GB, and do some action on that array, and ran 5 instances of this program vie forks.

The thing is, that I suspect that O.S killed 4 of them, and only one survived and displayed it's "PID: I've finished").

(I've tried it with small arrays and got 5 printing, also when I look at the running processes with TOP, I see only one instance..)

The weird thing is this - I've received return code 0 (success?) in ALL of the instances, including the ones that were allegedly killed by O.S.

I didn't get any massage stating that processes were killed.

Is this return code normal for this situation?

(If so, it reduces my trust in 'return codes'...)

thanks.

Edit: some of the answers suggested possible errors in the small program, so here it is. the larger program that forks and saves return codes is larger, and I have trouble uploading it here, but I think (and hope) it's fine.

Also I've noticed that if instead of running it with my forking program, I run it with terminal using './a.out & ./a.out & ./a.out & ./a.out &' (when ./a.out is the binary of the small program attached) I do see some 'Killed' messages.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#define SMALL_SIZE 10000
#define BIG_SIZE 1000000000
#define SIZE BIG_SIZE
#define REAPETS 1

    int
main()
{
    pid_t my_pid = getpid();

    char * x = malloc(SIZE*sizeof(char));

    if (x == NULL)
    {
            printf("Malloc failed!");
            return(EXIT_FAILURE);
    }

    int x2=0;
    for(x2=0;x2<REAPETS;++x2)
    {
            int y;
            for(y=0;y<SIZE;++y)
                    x[y] = (y+my_pid)%256;
    }
    printf("%d: I'm over.\n",my_pid);
    return(EXIT_SUCCESS);
}
like image 710
Liran Orevi Avatar asked Aug 21 '26 04:08

Liran Orevi


2 Answers

What signal was used to kill the processes?

Exit codes between 0 and 127, inclusive, can be used freely, and codes above 128 indicate that the process was terminated by a signal, where the exit code is

128 + the number of the signal used

like image 86
Mihai Limbășan Avatar answered Aug 22 '26 22:08

Mihai Limbășan


Well, if your process is unable to malloc() the 1GB of memory, the OS will not kill the process. All that happens is that malloc() returns NULL. So depending on how you wrote your code, it's possible that the process could return 0 anyway - if you wanted it to return an error code when a memory allocation fails (which is generally good practice), you'd have to program that behavior into it.

like image 26
David Z Avatar answered Aug 22 '26 21:08

David Z