wij
2024-11-19 13:05:40 UTC
libwy-0.69.3 is released.
https://sourceforge.net/projects/cscall/
An elementary C++ library by wrapping commonly used C library functions and
system calls into forms suitable for solving problems in fast and effective OO
way. The structure and API of this library are robust, almost fool-proof,
application normally just use it mechanically. This library added few for
developer to learn and remember in hopes that users can focus more on real
things, no need to endless learn things 'backwardly'.
------------------
This release added classes for forking process and memory mapping, because
the range of application of these two are much wider than threads (maybe safer
and easier, too). The following is an example program.
------------------------
/* Copyright is licensed by GNU LGPL, see file COPYING. by I.J.Wang 2024
Two proceeses increase one single int from 0 to 200 alternatively by using
mmap,semaphore
*/
#include <Wy.stdio.h>
#include <Wy.unistd.h>
#include <Wy.semaphore.h>
#include <CSCall/MemMap.h>
using namespace Wy;
int *shared_nptr=NULL;
Semaphore sem(Semaphore::InProcess,1);
// [Syn] Increase $shared_nptr pointer int from 0 to 200 depending on whether
// $v and the int are both odd, or even.
//
int ftask(int v) {
Errno r;
for(;;) {
if((r=sem.wait())!=Ok) {
WY_THROW(r);
}
if(*shared_nptr>200) {
break;
}
if((*shared_nptr&1)!=v) {
if((r=sem.post())!=Ok) {
WY_THROW(r);
}
continue;
}
cout << (v? 'o':'e') << *shared_nptr << ' ';
++*shared_nptr;
if((r=sem.post())!=Ok) {
WY_THROW(r);
}
}
return v;
};
int main(int argc, const char* argv[])
try {
Errno r;
MemMap mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED);
shared_nptr= static_cast<int*>(mmap.addr());
*shared_nptr=0;
ForkProcess fproc(ftask,1); // run ftask(1) in a new process
ftask(0);
if((r=fproc.wait())!=Ok) { // wait for change of state
WY_THROW(r);
}
cout << WY_ENDL "OK" WY_ENDL;
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "main() caught(...)" WY_ENDL;
throw;
};
https://sourceforge.net/projects/cscall/
An elementary C++ library by wrapping commonly used C library functions and
system calls into forms suitable for solving problems in fast and effective OO
way. The structure and API of this library are robust, almost fool-proof,
application normally just use it mechanically. This library added few for
developer to learn and remember in hopes that users can focus more on real
things, no need to endless learn things 'backwardly'.
------------------
This release added classes for forking process and memory mapping, because
the range of application of these two are much wider than threads (maybe safer
and easier, too). The following is an example program.
------------------------
/* Copyright is licensed by GNU LGPL, see file COPYING. by I.J.Wang 2024
Two proceeses increase one single int from 0 to 200 alternatively by using
mmap,semaphore
*/
#include <Wy.stdio.h>
#include <Wy.unistd.h>
#include <Wy.semaphore.h>
#include <CSCall/MemMap.h>
using namespace Wy;
int *shared_nptr=NULL;
Semaphore sem(Semaphore::InProcess,1);
// [Syn] Increase $shared_nptr pointer int from 0 to 200 depending on whether
// $v and the int are both odd, or even.
//
int ftask(int v) {
Errno r;
for(;;) {
if((r=sem.wait())!=Ok) {
WY_THROW(r);
}
if(*shared_nptr>200) {
break;
}
if((*shared_nptr&1)!=v) {
if((r=sem.post())!=Ok) {
WY_THROW(r);
}
continue;
}
cout << (v? 'o':'e') << *shared_nptr << ' ';
++*shared_nptr;
if((r=sem.post())!=Ok) {
WY_THROW(r);
}
}
return v;
};
int main(int argc, const char* argv[])
try {
Errno r;
MemMap mmap(NULL,sizeof(int),PROT_READ|PROT_WRITE,MAP_SHARED);
shared_nptr= static_cast<int*>(mmap.addr());
*shared_nptr=0;
ForkProcess fproc(ftask,1); // run ftask(1) in a new process
ftask(0);
if((r=fproc.wait())!=Ok) { // wait for change of state
WY_THROW(r);
}
cout << WY_ENDL "OK" WY_ENDL;
return 0;
}
catch(const Errno& e) {
cerr << wrd(e) << WY_ENDL;
return e.c_errno();
}
catch(...) {
cerr << "main() caught(...)" WY_ENDL;
throw;
};