Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Код первой программы:
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <iomanip>
- #include <semaphore.h>
- using namespace std;
- sem_t sem;
- void* func1(void* arg) {
- bool* f1 = (bool*)arg;
- while(*f1) {
- sem_wait(&sem);
- for(int i=0; i<3;i++){
- cout << "1" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- pthread_exit(0);
- }
- void* func2(void* arg) {
- bool* f2 = (bool*)arg;
- while(*f2) {
- sem_wait(&sem);
- for(int f=0; f<3;f++){
- cout << "2" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- pthread_exit(0);
- }
- int main() {
- bool f1 = true, f2 = true;
- void *status1, *status2;
- pthread_t thread1;
- pthread_t thread2;
- sem_init(&sem, 0, 1);
- pthread_create(&thread1, NULL, func1, &f1);
- pthread_create(&thread2, NULL, func2, &f2);
- getchar();
- f1 = false;
- f2 = false;
- pthread_join(thread1, &status1);
- pthread_join(thread2, &status2);
- sem_destroy(&sem);
- }
- Код второй программы:
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <iomanip>
- #include <semaphore.h>
- using namespace std;
- sem_t sem;
- void* func1(void* arg) {
- bool* f1 = (bool*)arg;
- while(*f1) {
- int rc=sem_trywait(&sem);
- if(rc==0){
- for(int i=0; i<3;i++){
- cout << "1" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- }
- pthread_exit(0);
- }
- void* func2(void* arg) {
- bool* f2 = (bool*)arg;
- while(*f2) {
- int rc=sem_trywait(&sem);
- if(rc==0){
- for(int f=0; f<3;f++){
- cout << "2" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- }
- pthread_exit(0);
- }
- int main() {
- bool f1 = true, f2 = true;
- void *status1, *status2;
- pthread_t thread1;
- pthread_t thread2;
- sem_init(&sem, 0, 1);
- pthread_create(&thread1, NULL, func1, &f1);
- pthread_create(&thread2, NULL, func2, &f2);
- getchar();
- f1 = false;
- f2 = false;
- pthread_join(thread1, &status1);
- pthread_join(thread2, &status2);
- sem_destroy(&sem);
- }
- Код третьей программы:
- #include <iostream>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <iomanip>
- #include <semaphore.h>
- using namespace std;
- sem_t sem;
- void* func1(void* arg) {
- struct timespec tp;
- bool* f1 = (bool*)arg;
- while(*f1) {
- clock_gettime(CLOCK_REALTIME, &tp);
- tp.tv_sec+=1;
- int rc=sem_timedwait(&sem,&tp);
- if(rc==0){
- for(int i=0; i<3;i++){
- cout << "1" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- }
- pthread_exit(0);
- }
- void* func2(void* arg) {
- struct timespec tp;
- bool* f2 = (bool*)arg;
- while(*f2) {
- clock_gettime(CLOCK_REALTIME, &tp);
- tp.tv_sec+=1;
- int rc=sem_timedwait(&sem,&tp);
- if(rc==0){
- for(int f=0; f<3;f++){
- cout << "2" << flush;
- sleep(1);
- }
- sem_post(&sem);
- sleep(1);
- }
- }
- pthread_exit(0);
- }
- int main() {
- bool f1 = true, f2 = true;
- void *status1, *status2;
- pthread_t thread1;
- pthread_t thread2;
- sem_init(&sem, 0, 1);
- pthread_create(&thread1, NULL, func1, &f1);
- pthread_create(&thread2, NULL, func2, &f2);
- getchar();
- f1 = false;
- f2 = false;
- pthread_join(thread1, &status1);
- pthread_join(thread2, &status2);
- sem_destroy(&sem);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement