I have a task to make a StarCraft like program with multiple pthreads as workers.
So , I have multiple pthreads that run the following function:
void* scv(int num){
int minerals_carried = 0;
while(map_minerals_remaining>0){
minerals_carried = 0;
for(int i = 0; i<number_of_fields; i++){
if(fields[i].minerals != 0 && minerals_carried == 0){
if(pthread_mutex_trylock(&fields[i].mutex)==0){
sleep(1);
// mine returns int
minerals_carried = mine(&fields[i]);
printf("SCV%d is carrying %d minerals from field %d\n",num,minerals_carried,i);
if(pthread_mutex_unlock(&fields[i].mutex)!=0){
perror("pthread_mutex_unlock");
return NULL;
}
}else{
perror("pthread_mutex_trylock");
return NULL;
}
}
}
}
return NULL;
}
I create 5 pthreads and they all get created properly , but only the first one prints out like its supposed to do, and all the other ones dont seem to do anything. Any idea why that might be ?
EDIT :
I was asked to show how I initialized number_of_fields and fields and this is it:
I first declare them as global
typedef struct Mineral_Field_t{
pthread_mutex_t mutex;
int minerals;
}Mineral_Field;
Mineral_Field* fields;
int number_of_fields = 2;
And then I have the following piece of code at the start of the main function:
if(argv[1] != NULL){
number_of_fields = atoi(argv[1]);
}
fields = malloc(number_of_fields*sizeof(Mineral_Field));
I'm trying to compare when does a given date corresponds to current time, when that happens it's supposed to execute a program. I have used an infinite loop so that it waits for the given time to correspond to the current time, the problem is that when that happens it executes the program more than one time and I don't know how to solve this...
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int taskexecution()
{
char * path;
path = "/home/soraia/mieti/Proj/makefile";
pid_t fk = fork();
if (!fk) { /* in child */
chdir("/home/soraia/mieti/Proj");
execlp ("make", "make", "-f", path , NULL);
_exit(127);
}
else if (fk == -1)
{
perror("fork"); /* print an error message */
}
return 0;
}
void time()
{
struct tm data;
data.tm_year=2015-1900;
data.tm_mon=1-1;
data.tm_mday=03;
data.tm_hour=10;
data.tm_min=49;
data.tm_sec=10;
data.tm_isdst = -1;
if(mktime(&data) == time(NULL))
{
taskexecution();
}
}
int main ()
{
while(1)
{
time();
}
return 0;
}
Your problem is that the computer runs so fast that your time() function can be called several times in the same second. What you need is to ensure that either your function stops the while loop after having run the task, or forbids the execution of the task:
First:
int time()
{
struct tm data;
data.tm_year=2015-1900;
data.tm_mon=1-1;
data.tm_mday=03;
data.tm_hour=10;
data.tm_min=49;
data.tm_sec=10;
data.tm_isdst = -1;
if (mktime(&data) == time(NULL))
{
taskexecution();
return 0; // returns 0 to stop while
}
return 1; // returns 1 to let the while continue
}
int main ()
{
while(time());
return 0;
}
second:
void time()
{
static int ran = 0; // static variable: 0 is task not already executed, 1 else
struct tm data;
data.tm_year=2015-1900;
data.tm_mon=1-1;
data.tm_mday=03;
data.tm_hour=10;
data.tm_min=49;
data.tm_sec=10;
data.tm_isdst = -1;
if(ran==0 && mktime(&data) == time(NULL))
{
taskexecution();
ran = 1; // Ok execution took place
}
}
I am trying to get a list of all the users in the system (linux, fedora).
and i've heard that the function:fgetpwent is the one that i need to that mission.
the sad part is that i didnt find any documentation or example of how to use this function.
if someone would give me an example, that would be great, thanks in advance.
No idea why I ever could have used it:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <crypt.h>
#include <pwd.h>
#include <sys/types.h>
char *testentry = "testread";
static void read_etc_passwd (void) {
struct passwd *pwd_entry = NULL;
int found = 0;
setpwent(); // go to the top of /etc/passwd
while (!found && (pwd_entry = getpwent())){
if (0 == strcmp (testentry, pwd_entry->pw_name)){
found = 1;
}
}
if (found) {
printf ("name = %s\nhome = %s\n", pwd_entry->pw_name,
pwd_entry->pw_dir);
} else {
puts("could not find the entry you were looking for, or"
"some error occurred");
}
}
void change_etc_passwd (void){
struct passwd *pwd = NULL;
FILE *pwd_fd = NULL;
FILE *pwd_new = NULL;
int result = 0;
pwd_fd = fopen ("/etc/passwd", "r");
pwd_new = fopen ("/tmp/passwd.neu", "a");
// assuming everthing went fine (bad idea)
while (pwd = fgetpwent (pwd_fd)){
if (0 == strcmp (pwd->pw_name, testentry)){
pwd->pw_passwd = crypt ("new_pwd", "aa");
}
result = putpwent(pwd, pwd_new);
if (result < 0){
fprintf (stderr, "Failed to write entry, giving up\n");
exit (EXIT_FAILURE);
}
}
}
int main (void) {
/* handling of /etc/passwd */
read_etc_passwd ();
change_etc_passwd();
return 0;
}
Add error handling and it may even work without breaking ;-)
I am watching a directory for file system events. Everything seems to work fine with one exception. When I create a file the first time, it spits out that it was created. Then I can remove it and it says it was removed. When I go to create the same file again, I get both a created and removed flag at the same time. I obviously am misunderstanding how the flags are being set when the callback is being called. What is happening here?
//
// main.c
// GoFSEvents
//
// Created by Kyle Cook on 8/22/13.
// Copyright (c) 2013 Kyle Cook. All rights reserved.
//
#include <CoreServices/CoreServices.h>
#include <stdio.h>
#include <string.h>
void eventCallback(FSEventStreamRef stream, void* callbackInfo, size_t numEvents, void* paths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
char **pathsList = paths;
for(int i = 0; i<numEvents; i++) {
uint32 flag = eventFlags[i];
uint32 created = kFSEventStreamEventFlagItemCreated;
uint32 removed = kFSEventStreamEventFlagItemRemoved;
if(flag & removed) {
printf("Item Removed: %s\n", pathsList[i]);
}
else if(flag & created) {
printf("Item Created: %s\n", pathsList[i]);
}
}
}
int main(int argc, const char * argv[])
{
CFStringRef mypath = CFSTR("/path/to/dir");
CFArrayRef paths = CFArrayCreate(NULL, (const void **)&mypath, 1, NULL);
CFRunLoopRef loop = CFRunLoopGetMain();
FSEventStreamRef stream = FSEventStreamCreate(NULL, (FSEventStreamCallback)eventCallback, NULL, paths, kFSEventStreamEventIdSinceNow, 1.0, kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer);
FSEventStreamScheduleWithRunLoop(stream, loop, kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
return 0;
}
As far as I can tell, you will have to look for either kFSEventStreamEventFlagItemRemoved or kFSEventStreamEventFlagItemCreated, and then use stat() or similar to check if the file was in fact added or deleted. The FSEvents documentation seems to hint as such.
It looks like the API is or'ing the events bits together... so really it's an OR of all the changes made since the FSEventsListener is created. Since that seems to be the case, another option might be to create a new FSEventListener each time (and use the coalesce timer option).
I did some Googling, but didn't find other examples of this problem or even apple sample code, but I didn't spend too long on it.
I have previously used the kqueue API: https://gist.github.com/nielsbot/5155671 (This gist is an obj-c wrapper around kqueue)
I changed your sample code to show all flags set for each FSEvent:
#include <CoreServices/CoreServices.h>
#include <stdio.h>
#include <string.h>
static int __count = 0 ;
void eventCallback(FSEventStreamRef stream, void* callbackInfo, size_t numEvents, void* paths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[]) {
char **pathsList = paths;
printf("callback #%u\n", ++__count ) ;
const char * flags[] = {
"MustScanSubDirs",
"UserDropped",
"KernelDropped",
"EventIdsWrapped",
"HistoryDone",
"RootChanged",
"Mount",
"Unmount",
"ItemCreated",
"ItemRemoved",
"ItemInodeMetaMod",
"ItemRenamed",
"ItemModified",
"ItemFinderInfoMod",
"ItemChangeOwner",
"ItemXattrMod",
"ItemIsFile",
"ItemIsDir",
"ItemIsSymlink",
"OwnEvent"
} ;
for(int i = 0; i<numEvents; i++)
{
printf("%u\n", i ) ;
printf("\tpath %s\n", pathsList[i]) ;
printf("\tflags: ") ;
long bit = 1 ;
for( int index=0, count = sizeof( flags ) / sizeof( flags[0]); index < count; ++index )
{
if ( ( eventFlags[i] & bit ) != 0 )
{
printf("%s ", flags[ index ] ) ;
}
bit <<= 1 ;
}
printf("\n") ;
}
FSEventStreamFlushSync( stream ) ;
}
int main(int argc, const char * argv[])
{
CFStringRef path = CFStringCreateWithCString( kCFAllocatorDefault, argv[1], kCFStringEncodingUTF8 ) ;
CFArrayRef paths = CFArrayCreate(NULL, (const void **)&path, 1, &kCFTypeArrayCallBacks );
if ( path ) { CFRelease( path ) ; }
CFRunLoopRef loop = CFRunLoopGetCurrent() ;
FSEventStreamRef stream = FSEventStreamCreate(NULL, (FSEventStreamCallback)eventCallback, NULL, paths, kFSEventStreamEventIdSinceNow, 0, kFSEventStreamCreateFlagFileEvents );
if ( paths ) { CFRelease( paths ) ; }
FSEventStreamScheduleWithRunLoop(stream, loop, kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun() ;
FSEventStreamStop(stream);
FSEventStreamInvalidate(stream);
FSEventStreamRelease(stream);
return 0;
}
i have an array of structures with fields of data and fields with pointers to functions.
what i'm doing now is cycling through array and calling each registered function.
what i need is for each of element in my structures array call registered function in a separate independent thread.
i can post an code example also if needed. sorry for my english :)
posting code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#define NTHREAD 3
struct server_t {
char *name;
int (*triggered)(struct server_t *);
};
typedef struct server_t server_t;
int triggered1(struct server_t * server)
{
if (time(NULL) % 1 == 0) {
printf("%s\n", __FUNCTION__);
pthread_exit(0);
return 0;
} else {
return -1;
}
}
int triggered2(struct server_t * server)
{
if (time(NULL) % 2 == 0) {
printf("%s\n", __FUNCTION__);
pthread_exit(0);
return 0;
} else {
return -1;
}
}
int triggered3(struct server_t * server)
{
if (time(NULL) % 5 == 0) {
printf("%s\n", __FUNCTION__);
pthread_exit(0);
return 0;
} else {
return -1;
}
}
int main()
{
pthread_t threads[NTHREAD];
int iret[NTHREAD]; int i = 0;
server_t servers[] = {
{"server1", triggered1},
{"server2", triggered2},
{"server3", triggered3},
};
/*
So, i have an array of structures. AND i have a main loop.
i want to create thread for each element of array, pass
structure's "triggered" function as start routine for it.
AND i need this start routine to periodically check for something.
So below some kind of an em.. code, that supposed to be.
*/
<create_threads(&servers);> // this function must create thread for each element of array
//with structure's "triggered" function as a start routine
//argument
/* after what threads are running and checking what they needed in an infinite loop. */
// ?maybe some code here?
return 0;
}
Here is a very good, basic pthreads example, which should get you going:
https://computing.llnl.gov/tutorials/pthreads/#CreatingThreads
Basically all you need to do is loop through your function-pointer-array and execute the functions according to the said example.