Difference between revisions of "LU-LSP-b13:L10"

From DiLab
Jump to: navigation, search
Line 40: Line 40:


* Izmantot funkciju ''fork()''.
* Izmantot funkciju ''fork()''.
* Izmantot funkciju ''scoketpair()''.
* Izmantot funkciju ''socketpair()''.





Revision as of 17:48, 26 November 2013

PD#10: Starprocesu komunikācija

Uzdevums

Realizēt komunikāciju starp parent un child progammām ar socket palīdzību.

Parent process padod child procesam ASCII rindiņu, kura satur divus skaitļus.

Child process rindiņu nolasa (piemēram, ar scanf funkciju), saskaita skatļus, izvada rezultātu (kā ASCII rindiņu) atpakaļ parentam.

Koda šablons:

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
    if (ret) {
	perror("socketpair");
	return 1;
    }


    // fork

    // in parent code: write to socket, wait for reply, read it, and output it


    // in child code: read from socket, process arguments, write reply, exit

    return 0;
}

Padomi

  • Izmantot funkciju fork().
  • Izmantot funkciju socketpair().


Iesūtīšana

Iesūtīt programmas C kodu uz kursa asistenta e-pastu.