.


:




:

































 

 

 

 





1. . . : . .: - . -, 2010. 180 .

2. , . . / . . .: , 2003. 877.

3. .., .., . 1. .: λ, 2007. 60 .

4. , .. , .: . .. .: , 2003. 512 .: . . . .

5. ru.wikipedia.org. : Oracle Solaris Studio https://ru.wikipedia.org/wiki/Oracle_Solaris_Studio.

6. www.opennet.ru. : Oracle Solaris Studio 12.4 Solaris Linux https://www.opennet.ru/opennews/art.shtml?num=32607.

7. http://www.oracle.com/technetwork/server-storage/developerstudio.

8. , .. Oracle Studio Performance Analyzer / .. : 2012. 15 .

9. http://bourabai.ru. : http://bourabai.ru/einf/ide.htm.


 

1.

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <pthread.h>

#include <unistd.h>

#include <malloc.h>

#include <semaphore.h>

 

int *mass;

int nom=0;

unsigned N=10;

sem_t semW,semR;

pthread_mutex_t lock;

void* funcR(void *arg)

{

int id_t=*(int *) arg;

printf(" Thread R %d Start \n",id_t);

while(1){

printf(" Thread R %d wait semaf\n",id_t);

sem_wait(&semR);

printf(" Thread R %d wait mutex\n",id_t);

pthread_mutex_lock(&lock);

if(nom>500){

pthread_mutex_unlock(&lock);

printf(" Thread R %d free mutex\n",id_t);

sem_post(&semW);

break;

}

for(int i=0;i<N;i++){

if(mass[i]==1) {

mass[i]=0;

break;

}

}

nom++;

printf(" Thread R %d wr 0\n",id_t);

pthread_mutex_unlock(&lock);

sleep(0.1);

printf(" Thread R %d free mutex\n",id_t);

sem_post(&semW);

printf(" Thread R %d free semaf\n",id_t);

}

}

void* funcW(void *arg)

{

int id_t=*(int *) arg;

printf(" Thread W %d Start \n",id_t);

while(1){

printf(" Thread W %d wait semaf\n",id_t);

sem_wait(&semW);

printf(" Thread W %d wait mutex\n",id_t);

pthread_mutex_lock(&lock);

if(nom>500){

pthread_mutex_unlock(&lock);

printf(" Thread W %d free mutex\n",id_t);

sem_post(&semR);

break;

}

for(int i=0;i<N;i++){

if(mass[i]==0) {

mass[i]=1;

break;

}

}

printf(" Thread W %d wr 1\n",id_t);

sleep(0.1);

pthread_mutex_unlock(&lock);

printf(" Thread W %d free mutex\n",id_t);

sem_post(&semR);

printf(" Thread W %d free semaf\n",id_t);

}

}

int main() {

pthread_t thR[4];

pthread_t thW[4];

int per;

mass=(int*) malloc(N*sizeof(int));

sem_init(&semW,0,3);

sem_init(&semR,0,0);

pthread_mutex_init(&lock,NULL);

for(int i=0;i<10;i++){

mass[i]=0;

}

for(int i=0;i<4;i++){

per = pthread_create(&thR[i],NULL, funcR,&i);

if(per!=0){

perror("error 111R");

return EXIT_FAILURE;

}

per = pthread_create(&thW[i],NULL, funcW,&i);

if(per!=0){

perror("error 111W");

return EXIT_FAILURE;

}

}

for(int i=0;i<4;i++){

pthread_join(thW[i],NULL);

pthread_join(thR[i],NULL);

}

free(mass);

pthread_mutex_destroy(&lock);

sem_destroy(&semW);

sem_destroy(&semR);

printf(" DONE\n");

return (EXIT_SUCCESS);

}

2.

#include <stdlib.h>

#include <stdio.h>

#include <errno.h>

#include <pthread.h>

#include <unistd.h>

 

pthread_mutex_t lock1;

pthread_mutex_t lock2;

int count=0;

 

void* thread_func1(void *arg)

{

int i;

int id_t=*(int *) arg;

printf(" Thread %d Start \n",id_t);

pthread_mutex_lock(&lock1);

pthread_mutex_lock(&lock2);

++;

//sleep(1);

pthread_mutex_unlock(&lock2);

pthread_mutex_unlock(&lock1);

sleep(1);

printf(" Thread %d Finish \n",id_t);

}

 

void* thread_func2(void *arg)

count

{

int i;

int id_t=*(int *) arg;

printf(" Thread %d Start \n",id_t);

pthread_mutex_lock(&lock2);

pthread_mutex_lock(&lock1);

count++;

sleep(1);

pthread_mutex_unlock(&lock1);

pthread_mutex_unlock(&lock2);

printf(" Thread %d Finish \n",id_t);

}

 

int main(int argc, char**argv) {

pthread_t th1,th2;

pthread_mutex_init(&lock1,NULL);

pthread_mutex_init(&lock2,NULL);

int id1=1,id2=2,result;

 

 

result = pthread_create(&th1,NULL, thread_func1,&id1);

if(result!=0){

perror("er 111");

return EXIT_FAILURE;

}

result = pthread_create(&th2,NULL, thread_func2,&id2);

if(result!=0){

perror("er 222");

return EXIT_FAILURE;

}

while(count<2){

;

}

 

/*

result= pthread_join(th1,NULL);

if(result!=0){

perror("join 111");

return EXIT_FAILURE;

}

result= pthread_join(th2,NULL);

if(result!=0){

perror("join 222");

return EXIT_FAILURE;

}*/

pthread_mutex_destroy(&lock1);

pthread_mutex_destroy(&lock2);

 

printf(" DONE\n");

return EXIT_SUCCESS;

}

 





:


: 2017-02-25; !; : 402 |


:

:

- , - .
==> ...

1496 - | 1412 -


© 2015-2024 lektsii.org - -

: 0.016 .