Код примера для 3 лабы
это главная программа:
/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
* Copyright (C) 2001 by New Riders Publishing *
* See COPYRIGHT for license information. *
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include "stdlib.h"
#include "time.h"
#include "unistd.h"
#include "sys/types.h"
#include <sys/wait.h>
#include "sys/ipc.h"
#include "sys/shm.h"
#include <signal.h>
/* Spawn a child process running a new program. PROGRAM is the name
of the program to run; the path will be searched for this program.
ARG_LIST is a NULL-terminated list of character strings to be
passed as the program's argument list. Returns the process id of
the spawned process. */
int spawn (char* program, char** arg_list)
{
pid_t child_pid;
/* Duplicate this process. */
child_pid = fork ();
if (child_pid != 0)
/* This is the parent process. */
return child_pid;
else {
/* Now execute PROGRAM, searching for it in the path. */
execvp (program, arg_list);
/* The execvp function returns only if an error occurs. */
fprintf (stderr, "an error occurred in execvp\n");
abort ();
}
}
int main ()
{
srand(time(NULL));
int k,i,n,N=10,smid1,smid2,smid3,*x,*y,*h;
smid1 = shmget(IPC_PRIVATE, sizeof(int)*N, IPC_CREAT | 0666);
x = (int*) shmat(smid1, (void*) NULL, 0);
smid2 = shmget(IPC_PRIVATE, sizeof(int)*N, IPC_CREAT | 0666);
y = (int*) shmat(smid2, (void*) NULL, 0);
smid3 = shmget(IPC_PRIVATE, sizeof(int)*N, IPC_CREAT | 0666);
h = (int*) shmat(smid3, (void*) NULL, 0);
for(i=0; i<N; i++){
y[i]=0;
x[i]=rand()%10;
h[i]=rand()%10;
}
/* The argument list to pass to the "ls" command. */
char x1[16],x2[16],x3[16],x4[16],x5[16];
sprintf(x1,"%i",smid1);
sprintf(x2,"%i",smid2);
sprintf(x3,"%i",smid3);
sprintf(x4,"%i",0);
sprintf(x5,"%i",(N/3));
char* arg_list[] = {
"/home/student/Загрузки/helo", /* argv[0], the name of the program. */
x1,
x2,
x3,
x4,
x5,
NULL /* The argument list must end with a NULL. */
};
/* Spawn a child process running the "ls" command. Ignore the
returned child process id. */
//------------------------------------------------------------------------------------
spawn ("/home/student/Загрузки/helo", arg_list);
sprintf(x1,"%i",smid1);
sprintf(x2,"%i",smid2);
sprintf(x3,"%i",smid3);
sprintf(x4,"%i",N/3);
sprintf(x5,"%i",2*(N/3));
char* arg_list1[] = {
"/home/student/Загрузки/helo", /* argv[0], the name of the program. */
x1,
x2,
x3,
x4,
x5,
NULL /* The argument list must end with a NULL. */
};
spawn ("/home/student/Загрузки/helo", arg_list1);
//=======================================================================================
sprintf(x1,"%i",smid1);
sprintf(x2,"%i",smid2);
sprintf(x3,"%i",smid3);
sprintf(x4,"%i",2*(N/3));
sprintf(x5,"%i",N);
char* arg_list2[] = {
"/home/student/Загрузки/helo", /* argv[0], the name of the program. */
x1,
x2,
x3,
x4,
x5,
NULL /* The argument list must end with a NULL. */
};
spawn ("/home/student/Загрузки/helo", arg_list2);
printf ("done with main program\n");
sleep(2);
printf("Convolution:");
for(i=0; i<N; i++)
printf("%i ",y[i]);
return 0;
}
Дочерняя:
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv)
{
printf("\n helo");
int n, smid1,smid2,smid3,*x,*y,*h,S,F;
smid1=atoi(argv[1]);
smid2=atoi(argv[2]);
smid3=atoi(argv[3]);
//printf("%i %i %i",smid1, smid2,smid3);
S=atoi(argv[4]);
F=atoi(argv[5]);
x = (int*) shmat(smid1, (void*) NULL, 0);
y = (int*) shmat(smid2, (void*) NULL, 0);
h = (int*) shmat(smid3, (void*) NULL, 0);
do
{
n=0;
do{
y[S]+= h[n] * x[S-n];
n++;
}while(n<=S);
S++;
} while(S<=F);
}
P.S.: Имя исполняемого файла дочерней программы "helo"