Barts_706
No więc spróbowałem zrobić programik testowy do użycia semaforów, ale tworzonych ciut inaczej:
#include <unistd.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <semaphore.h>
#include <fcntl.h>
std::string timePrint()
{
pid_t pid = getpid();
std::ostringstream os;
time_t now = time(NULL);
struct tm localTime;
memset (&localTime, 0, sizeof(struct tm));
localtime_r(&now, &localTime);
os << std::setw(4) << std::setfill('0') << localTime.tm_year
<< "-" << std::setw(2) << std::setfill('0') << localTime.tm_mon
<< "-" << std::setw(2) << std::setfill('0') << localTime.tm_mday
<< " " << std::setw(2) << std::setfill('0') << localTime.tm_hour
<< ":" << std::setw(2) << std::setfill('0') << localTime.tm_min
<< ":" << std::setw(2) << std::setfill('0') << localTime.tm_sec
<< " [" << pid << "]: ";
std::string ret(os.str());
return ret;
}
int main_semopen(int argc, char **argv);
int main(int argc, char **argv)
{
char semName[] = "/dssv2-updatethread-semaphore";
sem_t *semId = sem_open(semName, O_CREAT, 0666, 0);
if (semId == SEM_FAILED)
{
std::cout << timePrint() << "Unable to create a semaphore... :(" << std::endl;
perror("Semaphore creation problem");
exit(0);
}
std::cout << timePrint() << "Processing started" << std::endl;
srand(time(NULL));
double maxRandomTime = 5.0;
for (unsigned int passCounter = 0; passCounter < 10; ++passCounter)
{
int randomPossessTime = 3 + static_cast<int>(maxRandomTime * rand() / (RAND_MAX + 1.0));
std::cout << timePrint() << "Pass: " << passCounter << std::endl;
std::cout << timePrint() << "Trying to get semaphore..." << std::endl;
std::cout << timePrint() << "Trying to sync on semaphore..." << std::endl;
sem_wait(semId);
std::cout << timePrint() << "Semaphore set up, waiting '" << randomPossessTime << "' seconds..." << std::endl;
sleep(randomPossessTime);
std::cout << timePrint() << "sleep finished, reverting the semaphore state" << std::endl;
std::cout << timePrint() << "Trying to revert the semaphore..." << std::endl;
sem_post(semId);
std::cout << timePrint() << "Semaphore reverted, sleeping two seconds.." << std::endl;
sleep(2);
}
std::cout << timePrint() << "Processing ended" << std::endl;
return 0;
}
No i nadal nie bangla:
[hybd05]:threading> tail -f result.txt
Test started
0108-05-20 04:22:07 [26824]: Processing started
0108-05-20 04:22:07 [26824]: Pass: 0
0108-05-20 04:22:07 [26824]: Trying to get semaphore...
0108-05-20 04:22:07 [26824]: Trying to sync on semaphore...
0108-05-20 04:22:07 [26825]: Processing started
0108-05-20 04:22:07 [26825]: Pass: 0
0108-05-20 04:22:07 [26825]: Trying to get semaphore...
0108-05-20 04:22:07 [26825]: Trying to sync on semaphore...
W dodatku komenda ipcs nie pokazuje żadnych semaphore arrays...
W.T.F. ???
Ktoś ma jakis pomysł zanim zwątpię w moje zdolności programistyczne?
#include <unistd.h>
#include <sys/types.h>
#include <sys/sem.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <semaphore.h>
#include <fcntl.h>
std::string timePrint()
{
pid_t pid = getpid();
std::ostringstream os;
time_t now = time(NULL);
struct tm localTime;
memset (&localTime, 0, sizeof(struct tm));
localtime_r(&now, &localTime);
os << std::setw(4) << std::setfill('0') << localTime.tm_year
<< "-" << std::setw(2) << std::setfill('0') << localTime.tm_mon
<< "-" << std::setw(2) << std::setfill('0') << localTime.tm_mday
<< " " << std::setw(2) << std::setfill('0') << localTime.tm_hour
<< ":" << std::setw(2) << std::setfill('0') << localTime.tm_min
<< ":" << std::setw(2) << std::setfill('0') << localTime.tm_sec
<< " [" << pid << "]: ";
std::string ret(os.str());
return ret;
}
int main_semopen(int argc, char **argv);
int main(int argc, char **argv)
{
char semName[] = "/dssv2-updatethread-semaphore";
sem_t *semId = sem_open(semName, O_CREAT, 0666, 0);
if (semId == SEM_FAILED)
{
std::cout << timePrint() << "Unable to create a semaphore... :(" << std::endl;
perror("Semaphore creation problem");
exit(0);
}
std::cout << timePrint() << "Processing started" << std::endl;
srand(time(NULL));
double maxRandomTime = 5.0;
for (unsigned int passCounter = 0; passCounter < 10; ++passCounter)
{
int randomPossessTime = 3 + static_cast<int>(maxRandomTime * rand() / (RAND_MAX + 1.0));
std::cout << timePrint() << "Pass: " << passCounter << std::endl;
std::cout << timePrint() << "Trying to get semaphore..." << std::endl;
std::cout << timePrint() << "Trying to sync on semaphore..." << std::endl;
sem_wait(semId);
std::cout << timePrint() << "Semaphore set up, waiting '" << randomPossessTime << "' seconds..." << std::endl;
sleep(randomPossessTime);
std::cout << timePrint() << "sleep finished, reverting the semaphore state" << std::endl;
std::cout << timePrint() << "Trying to revert the semaphore..." << std::endl;
sem_post(semId);
std::cout << timePrint() << "Semaphore reverted, sleeping two seconds.." << std::endl;
sleep(2);
}
std::cout << timePrint() << "Processing ended" << std::endl;
return 0;
}
No i nadal nie bangla:
[hybd05]:threading> tail -f result.txt
Test started
0108-05-20 04:22:07 [26824]: Processing started
0108-05-20 04:22:07 [26824]: Pass: 0
0108-05-20 04:22:07 [26824]: Trying to get semaphore...
0108-05-20 04:22:07 [26824]: Trying to sync on semaphore...
0108-05-20 04:22:07 [26825]: Processing started
0108-05-20 04:22:07 [26825]: Pass: 0
0108-05-20 04:22:07 [26825]: Trying to get semaphore...
0108-05-20 04:22:07 [26825]: Trying to sync on semaphore...
W dodatku komenda ipcs nie pokazuje żadnych semaphore arrays...
W.T.F. ???
Ktoś ma jakis pomysł zanim zwątpię w moje zdolności programistyczne?
