Understanding IP Addresses
Understanding Subnets and CIDR
Audio Video Tools for Linux
Packing structures \ enums
There are various ways in which this can be done.
1. using #pragma pack()
This would pack the structure on a 2 byte boundary. If a tight packing is required use
2. Using -fpack-struct
Instead of using the
2. Using __attribute__ ((__packed__))
Compile the code normally.
We can also do it this way
1. using #pragma pack()
#pragma pack(2)
typedef struct
{
char c;
int i;
} DataType;
#pragma pack()This would pack the structure on a 2 byte boundary. If a tight packing is required use
#pragma pack(1) instead. Compile it normally using gcc.2. Using -fpack-struct
Instead of using the
#pragma, we can directly use the compiler flags instead.$gcc -Wall -fpack-struct -fshort-enums test.c -o test This would pack all the structs to the 1 byte boundary and consider shorts for enums instead of integers2. Using __attribute__ ((__packed__))
typedef struct
{
char c;
int i;
} __attribute__ ((__packed__))DataType;
.Compile the code normally.
We can also do it this way
typedef struct
{
char c __attribute__ ((__packed__));
int i1 __attribute__ ((__packed__));
int i2;
} DataType;
APR error
If you are using APR (Apache portable runtime) and on compilation of your code you get the following error
Do this,
which returns the following on my system
Use these while compiling your code.
apr expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘apr_off_t’Do this,
$ apr-config --cppflags --cflags which returns the following on my system
-DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -pipe -Wall -g -O2 -pthread Use these while compiling your code.
gcc file.c -o file -I/usr/include/apr-1.0 -lapr-1 `apr-config --cppflags --cflags`
Using POSIX Message Queues
Code
/* Includes */
#include < pthread.h >
#include < stdio.h >
#include < stdlib.h >
#include < unistd.h >
#include < mqueue.h >
#include < sys/stat.h >
#include < errno.h >
#include < string.h >
/* Threads */
static void *MainThread (void *);
static void *AnotherThread (void *);
/* Defines */
#define MAIN_QNAME "/MainQueue"
pthread_mutex_t wait_mutex;
int mq_test(void)
{
pthread_t mainThread, anotherThread;
printf ("Creating threads .. \n");
pthread_create (&mainThread, NULL, &MainThread, NULL);
pthread_create (&anotherThread, NULL, &AnotherThread, NULL);
pthread_mutex_init (&wait_mutex, NULL);
pthread_join (mainThread, NULL);
pthread_join (anotherThread, NULL);
return 1;
}
#define BUFFER_SIZE 10000
/* Main thread .. Waiting for messages */
static void *MainThread (void *args)
{
mqd_t queue_handle;
char buffer[BUFFER_SIZE];
int bytes_read;
struct mq_attr msgq_attr;
unsigned int sender;
printf ("[MainThread] Inside main thread \n");
// Let the other thread wait till I am ready!
pthread_mutex_lock (&wait_mutex);
// Clear the buffer
memset (buffer, 0, BUFFER_SIZE);
// Detach the thread
pthread_detach (pthread_self());
// unlink the queue if it exisits - debug
mq_unlink (MAIN_QNAME);
printf ("[MainThread]Opening MQ \n");
queue_handle= mq_open(MAIN_QNAME, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG, NULL);
if (queue_handle == -1)
{
perror ("[MainThread] Error Opening MQ: ");
return 0;
}
printf ("[MainThread] Waiting for messages ... \n");
pthread_mutex_unlock (&wait_mutex);
for (; ;)
{
bytes_read = mq_receive(queue_handle, buffer, BUFFER_SIZE, &sender);
if (bytes_read == -1)
{
perror("[MainThread] Failed to recieve:");
return 0;
}
else
{
printf ("[MainThread] Data: %s \n", buffer);
// Get the MQ attributes
mq_getattr(queue_handle, &msgq_attr);
printf("[MainThread] Queue \"%s\":\n"
"\t- stores at most %ld messages\n"
"\t- large at most %ld bytes each\n"
"\t- currently holds %ld messages\n",
MAIN_QNAME,
msgq_attr.mq_maxmsg,
msgq_attr.mq_msgsize,
msgq_attr.mq_curmsgs);
// Clear buffer and sleep to block for some time t and see
// if you get all the messages!
memset(buffer, 0, BUFFER_SIZE);
sleep(5);
}
}
mq_close (queue_handle);
}
#define MAX_SEND_BUFFER 70
static void *AnotherThread (void *args)
{
mqd_t queue_handle;
char buffer[MAX_SEND_BUFFER];
unsigned int msgprio = 1;
int count;
printf ("[AnotherThread] Inside Another thread \n");
pthread_mutex_lock (&wait_mutex);
queue_handle= mq_open(MAIN_QNAME, O_RDWR);
if (queue_handle == -1)
{
perror ("[AnotherThread] Error Opening MQ:");
return 0;
}
for (count = 0; count < 100; count++)
{
snprintf (buffer, MAX_SEND_BUFFER, "Message %d from Another thread", count);
if (0 != mq_send (queue_handle, buffer, strlen(buffer)+1, msgprio))
{
perror ("[AnotherThread] Sending:");
mq_close (queue_handle);
pthread_mutex_unlock (&wait_mutex);
return 0;
}
}
pthread_mutex_unlock (&wait_mutex);
mq_close (queue_handle);
return 0;
}
#ifdef _DEBUG_
int main (void)
{
return mq_test(void);
}
#endif
grep - argument list too long
If you are doing
Instead use
$ grep "text" `find . -type f` this would result in a long argument list that grep can't handle and it would give an error saying Argument list too long Instead use
find . -type f | xargs egrep "text"
Using smbclient from command prompt
If you are using *ubuntu, change to the root bash shell
You can use ? or help to get the commands which are similar to ftp like cd, lcd, mget, mput and so on
$sudo bash
Password: [Your kubuntu password]
$smbclient \\\\169.12.10.184\\SHARE_NAME -U DOMAIN\\USERNAME
Password: [Your windows password]
smb: \> help
You can use ? or help to get the commands which are similar to ftp like cd, lcd, mget, mput and so on
Virtualization to a next level
coLinux is a port of the Linux Kernel to Windows which executes as a single process. Using Xming server one can easily run X apps on windows. IBM Developer works has an article on the same topic:
Virtualization with coLinux .. read
One of the best applications of
coLinux is andLinux which allows you to run Ubuntu Linux seamlessly on Windows 2000/XP/Vista machines .. read.LifeHacker has an article on
andLinux .. read
Subscribe to:
Posts (Atom)