Programming Shared Address Space Machines

yakconspiracyΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 3 μήνες)

99 εμφανίσεις

Programming

Shared Address Space
Machines

Process

is an address space + current state

C
urrent state

Threads are process share the same address space.

T
hreads allow a concurrent programming model
within a single process, with very rap
id switching
of control of cpu from one thread to another
possible because little or no memory management
is involved.

I
ndividual threads are not visible outside a process.


A
dvantages

1.

threads

provide a natural implementation of
nonblocking communication
operations.

2.

threads can increase the
convenience

and
efficiency of
implementation of collective
operaions.

3.

threads are becoming the parallel
programming

model of choice.


T
he address space in
UNIX

is partitioned into the
user space and
kernel

space.

A
ll ac
ces
ses to the kernel space must mak
e via
system calls, which are to protect one process from
another.




U
nix Multi
-
process Codes.

pid = fork()

returns 0 to the child and pid of child to parent,

and return
-
1 on error.

W
ait(NUL
L) return
the pid of the child
.

unixthd.c

#include <stdio.h>

#include <sys/wait.h>

#include <sys/types.h>

/*#include <unistd.h>*/

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

{


pid_t pid;


int i,j;


if((pid=fork())<0)


{


printf("fork gailed
\
n");


exit(1);



}


if(pid==0){


printf("I am the child
\
n");


for(i=0;i<10;i++)printf("waiting
\
n");


exit(2);


}


else


{


printf("I am the parent with child %d
\
n",pid);


while(wait(NULL)!=pid);


wait(NULL);


printf("Child is now done
\
n
");


exit(0);


}

}






S
hmget function gets a shared memory segment
and its
identifier
.

partsum.c

#include <stdio.h>

#include <sys/wait.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <memory.h>

#in
clude <string.h>

#include <stdlib.h>

#define NUMPROCS 4

#define TABLE_SIZE 8


main()

{


int pid;


pid_t pidt;


int sum_id,partial_sum_id,global_sum;


/* int key=getpid();*/


int *sum_array,*partial_sum_array;



int i;


int local_table_size,start;



/* get shared memory */


if((sum_id=shmget((key_t) IPC_PRIVATE,TABLE_SIZE*sizeof(int),




0666|IPC_CREAT))==
-
1)


{


printf("shmget error in sort table
\
n");


exit(
-
1);


}


if((partial_sum_id=shmget((key_t) IPC_PRIVATE,





NUMPROCS
*sizeof(int),





0666|IPC_CREAT))==
-
1)


{


printf("shmget error in lock table
\
n");


exit(
-
1);


}




/*attch shared memory */


if((sum_array=(int *)shmat(sum_id,(void *)0,0))


==(int *)(
-
1))


{


printf("shmat error
\
n");


exit(
-
1);


}


if((partial_sum_array=(int *)shmat(partial_sum_id,(void *)0,0))


==(int *)(
-
1))


{


printf("shmat error
\
n");


exit(
-
1);


}


for(i=0;i<NUMPROCS;i++)


partial_sum_array[i]=0;


local_table_size=TABLE_SIZE/NUMP
ROCS;



for(i=0;i<TABLE_SIZE;i++)


sum_array[i]=i;



for(pid=0;pid<3;pid++){


if((pidt=fork())==0){


break;


}


printf("child's pid %d
\
n",pidt);


}


printf("
\
n");


start=local_table_size*pid;


for(i=start;i<start+local_table_size;i++
)


partial_sum_array[pid]+=sum_array[i];




if(pid==3){


for(i=0;i<NUMPROCS
-
1;i++)


printf("child's pid %d
\
n",wait(NULL));


global_sum=0;


for(i=0;i<NUMPROCS;i++){


printf("sum %d %d
\
n",partial_sum_array[i], pid);


global_sum+
=partial_sum_array[i];


}


printf("SUM %d
\
n",global_sum);


}

}

Rmks:1.Security requirements:


One process must not be able to intenti
o
nally
interfere with another.


2.Pains in the butt.

0x666: 6=rwx

IPC_CREAT
---
creat anyway

IPC_EXCL
---
will n
ot creat

IPC_PRIVATE default value












T
hreads

M
ultiple threads generated by a single processor
share the memory space, the code, and all devices
and file handlers.

T
he only data private to the thread is the stack and
the registers.

S
ince threads h
ave a much smaller context than
processes, switching threads is significantly fast.







W
hy threads

S
oftware
Portability
:

L
atency
Hiding:

mask memory latency

Scheduling

and load Balancing

S
ynchronization and shared variables:

lock

Eas
e of programming:

POSIX
Threads (
P threads)

IEEE specified a
standard

thread at 1995.

pthdsum.c

#define NUMTHREADS 4

#define NUM_ELEMENTS 16

#include <pthread.h>

#include <stdio.h>

#include <stdlib.h>

struct array_descriptor{


int thread_number;


int *ar
ray;


int length;

};

void *initialize_and_sum_array(void *start);

int array[NUM_ELEMENTS],partial_sum[NUMTHREADS];

main()

{


pthread_t threads[NUMTHREADS];


struct array_descriptor desc[NUMTHREADS];


int i,j,global_sum,index;


for(i=0;i<NUMTHREADS;i++
) partial_sum[i]=0;


global_sum=0;



pthread_init();



/* setup array here and spawn threads*/


for(i=0;i<NUMTHREADS;i++){


desc[i].thread_number=i;


desc[i].array=&(array[i*NUM_ELEMENTS/NUMTHREADS]);


desc[i].length=NUM_ELEMENTS/NUMTHREADS;


pthread_create(&threads[i],NULL,initialize_and_sum_array,




(void *)&desc[i]);


}



/*wait for everybody to end now */


for(i=0;i<NUMTHREADS;i++){


index=pthread_join(threads[i],NULL);


printf("index %d
\
n",index);


}



for(i=0;i<NUMTHREA
DS;i++){


global_sum+=partial_sum[i];


printf("%d
\
n",partial_sum[i]);


}


printf("sum %d
\
n",global_sum);

}

void *initialize_and_sum_array(void *s)

{


int *array,length;


int i,thread_no;




thread_no=((struct array_descriptor *)s)
-
>thread_nu
mber;


array=((struct array_descriptor *)s)
-
>array;


length=((struct array_descriptor *)s)
-
>length;


for(i=0;i<length;i++)


array[i]=thread_no*NUM_ELEMENTS/NUMTHREADS+i;


for(i=0;i<length;i++)


partial_sum[thread_no]+=array[i];

}