473,653 Members | 2,990 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

opening mouse dev's with multiple threads

1 New Member
hello everyone,

i've been trying to write something that checks the inputs from the possible devices where a mouse can be connected (debian/linux) in order to make it configure the system according to the right path

i'm proceeding as follow:
i call a function that will invoke multiple threads, each one trying to get some input stream from a device that is declared on a configuration file. i can get the first one (/dev/psaux as it is configured now) to read correctly and it even print those funny character on the screen. the problem is when it gets to the second. guess what, seg fault.

if i take the same device and open it on a small piece of code just for testing, it can open with no seg fault, but with no input, just as it is expected, because it was supposed to be the serial port (/dev/ttyS0)

i've tried lots of thing, such as fscanf and fgetc, the closest shot being the below-presented fread_unlocked

i'm posting the function that is getting me into trouble, any more details you need please contact me

thanks a lot

Expand|Select|Wrap|Line Numbers
  1. void *getStream(pdevices dev, int i, pthread_mutex_t mutex) {
  2.     FILE *device;
  3.     FILE *log;
  4.     void *inStream;
  5.     char *timeStamp;
  6.     size_t inSize;
  7.     pthread_t self;
  8.  
  9.     printf("[DEBUG] i = %i | dev->devicesArray[i] = %i\n", i, dev->devicesArray[i]);
  10.     if (dev->devicesArray[i]) {
  11.         self = pthread_self();
  12.  
  13.         device = fopen(dev->devicesArrayString[i], "r");
  14.  
  15.         if((inStream = malloc(100 * sizeof(char))) == NULL) {
  16.             puts("Memory allocation error");
  17.             exit(1);
  18.         }
  19.  
  20.         printf("[DEBUG] dev->devicesArrayString[i] = %s\n", dev->devicesArrayString[i]);
  21.         inSize = fread_unlocked(inStream, sizeof(char), 99, device);
  22.         printf("[DEBUG] Size of inSize = %i\n", (int) inSize);
  23.  
  24.         if(inSize < 0) {
  25.             APPEND_LOG(log);
  26.             TIMESTAMP(timeStamp);
  27.             fprintf(log, "%s No entries on device %s\n", timeStamp, dev->devicesArrayString[i]);
  28.             FREE(timeStamp);
  29.             CLOSE(log);
  30.             puts("[DEBUG] Nothing read!");
  31.         }
  32.         printf("[DEBUG] inStream = %s\n", (char *)inStream);    
  33.         if (inStream != NULL) {
  34.             dev->devicesArray[i] = 1;
  35.             if (pthread_mutex_lock(&mutex)) {
  36.                 fprintf(stderr, "mutex lock error\n");
  37.                 exit(1);
  38.             }
  39.             APPEND_LOG(log);
  40.             TIMESTAMP(timeStamp);
  41.             fprintf(log, "%s Input on device %s: OK\n", timeStamp, dev->devicesArrayString[i]);
  42.             FREE(timeStamp);
  43.             TIMESTAMP(timeStamp);
  44.             fprintf(log, "%s %s: %d\n", timeStamp, dev->devicesArrayString[i], (int) self);
  45.             FREE(timeStamp);
  46.             CLOSE(log);
  47.             if(pthread_mutex_unlock(&mutex)) {
  48.                 fprintf(stderr, "mutex unlock error\n");
  49.                 exit(1);
  50.             }
  51.         } else {
  52.             dev->devicesArray[i] = 0;
  53.         }
  54.         FREE(inStream);
  55.         CLOSE(device);
  56.     }
  57.     return 0;
  58. }
Oct 22 '07 #1
1 1622
RRick
463 Recognized Expert Contributor
You never check the result from fopen. If fopen encounters a problem, it will return a NULL for the file pointer. Any use with a null file pointer is crash and burn time.

You can also use the system level open (for more info, try man 2 open). This has some extra features that might be useful. If you are successful with open, you can create a FILE * using fdopen with the file descriptor from open.
Nov 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.