Please disable adblock to view this page.

← Go home

Multicore Programming – using posix_spawn() to create processes (C++)

April 24, 2016
Published By : Pratik Kataria
Categorised in: ,

/*
Using multicore programming implement posix_spawn() function to create a process.
*/

/*Execution:
oct@CCOMPL08-10:~$ g++ templ.cpp -o mat.o
oct@CCOMPL08-10:~$ g++ multicore.cpp -lpthread
oct@CCOMPL08-10:~$ ./a.out
*/

#include<iostream>
#include<spawn.h>
#include<sys/types.h>
#include<sys/wait.h>

using namespace std;

int main(int argc, char *argv[], char *env[]){

    pid_t pid, pid1;
    int status, status1;
    
    status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, env);
    cout<<"------------------------ \n";
    if(status == 0) cout<<"Process created successfully... \n";
    cout<<"------------------------ \n";
    wait(&pid);
    
    status1 = posix_spawn(&pid1, "mat.o", NULL, NULL, argv, env);
    cout<<"------------------------ \n";
    if(status1 == 0) cout<<"Process created successfully... \n";
    cout<<"------------------------ \n";
    
    wait(&pid1);
    
    return 0;

}

View Article Page
Download