Difference In Output When Using Pthread_join??

For the below code, I observe that when pthread_join is commented from the code - the order of thread execution is not consistent (the main thread executes first and then the remaining threads - in what sequence I am not sure). But when pthread_join is present in the code - I see that order of execution of the threads is as per the code (First t1, then t2 and finally main exits....I can tell that for sure by observing the increasing value of id in main())

Can anybody tell me why that happens?


#include <pthread.h>
#include <stdio.h>
pthread_once_t once = PTHREAD_ONCE_INIT;

void *myinit()
{
printf("\n I am in init\n");
}

void* t1routine(void* i)
{
printf("\n I am in thread%d\n", *(int*)i);
pthread_once(&once, (void*)myinit);
printf("\n I am exiting from thread %d\n", *(int*)i);
}

void* t2routine(void* i)
{
printf("\n I am in thread%d\n", *(int*)i);
pthread_once(&once, (void*)myinit);
printf("\n I am exiting from thread %d\n", *(int*)i);
}

void* t3routine(void* i)
{
printf("\n I am in thread%d\n", *(int*)i);
pthread_once(&once, (void*)myinit);
printf("\n I am exiting from thread %d\n", *(int*)i);
}

main()
{
int id = 1;
pthread_t t1,t2,t3;
pthread_create(&t1, NULL, t1routine, (void*) &id);
pthread_join(t1, NULL);
id++;

pthread_create(&t2, NULL, t2routine, (void*) &id);
pthread_join(t2, NULL);
id++;

pthread_create(&t3, NULL, t3routine, (void*) &id);
pthread_join(t3, NULL);
pthread_exit(NULL);
}


Similar Content



Difference Between These Function Calls In A Routing Protocol In Ns2

Could you please tell me the difference between agent->function name() vs this->function name ().
Actually i am taking about LAR.cc file (LAR routing protocol) but these function calls can be part of any routing protocol.

As you can see below , they are calling the same function with this and agent in LAR.cc . What is the difference between when we are calling a function with "agent" or calling with "this"

>
> void LARRouteRequestTimer::expire(Event *e)
> {
> ......
> .....
> agent->sendRouteRequest(this->dest, forwardMethod, this->routeInfo, false); //ringZero=false
> .....
> ...
> }
>
>
> void LARAgent::routeRequest(Packet *p)
> {
> .....
> ....
> ...
> this->sendRouteRequest(dest, larhdr->forwardCode(), &(destPtr->second), useRingZeroSearch);
> .....
> .....
> }

I have to do some changes in this file . kindly give some information on this issue .

Problems With "error: Invalid Conversion From `const Char*' To `char*' "

Hi, I need some help. I tried to install obs-ns. It´s based on ns2, but when I ran "make", the problem below appears... please I wish anybody would help me.


This is the error:

Code:
OBS/ns-2/integrated_agent.h: In constructor `BurstManager::BurstManager()':
OBS/ns-2/integrated_agent.h:86: warning: `BurstManager::bt_' will be initialized after
OBS/ns-2/integrated_agent.h:84: warning:   `int BurstManager::currburstsize_'
OBS/ns-2/integrated_agent.cc:66: warning:   when initialized here
OBS/ns-2/integrated_agent.cc: In member function `int BurstManager::nhops(nsaddr_t, nsaddr_t)':
OBS/ns-2/integrated_agent.cc:81: error: invalid conversion from `const char*' to `char*'   <------- this is the error 
OBS/ns-2/integrated_agent.cc: In member function `virtual void IPKTAgent::recv(Packet*, Handler*)':
OBS/ns-2/integrated_agent.cc:180: warning: unused variable 'ipkth'
/root/OBS/ns-allinone-2.31/tclcl-1.19/tclcl.h:150: error: `void Tcl::error(const char*)' is private
OBS/ns-2/integrated_agent.cc:185: error: within this context
OBS/ns-2/integrated_agent.cc:155: warning: unused variable 'hdr'
OBS/ns-2/integrated_agent.cc: In member function `void IPKTAgent::deBurst(Packet*)':
OBS/ns-2/integrated_agent.cc:192: warning: unused variable 'hdrip'
OBS/ns-2/integrated_agent.cc:194: warning: unused variable 'ch'
make: *** [OBS/ns-2/integrated_agent.o] Error 1


And this is the code of the program...


Code:
int BurstManager::nhops(nsaddr_t src,nsaddr_t des) {
  Tcl& tcl = Tcl::instance();
  sprintf(tcl.buffer(),"$ns nhops %d %d",src,des);
  tcl.eval();
  char *ni = tcl.result();   <------this is the line 81 
  return atoi(ni);
}

After Install Gpsr, Test The Perimeter Mode , It Has Throw "Wrong The Other Node"

hello there!
the common communication between nodes is fine, but when i want to test the perimeter mode it has throw the error "Wrong the other node", it seems that it couldn't find its next hop node.
i trace the code as show below
Code:
int
GPSRNeighbors::intersect(nsaddr_t theother, double sx, double sy,
			 double dx, double dy){
  //line 1 (x1,y1)--(x2,y2) is the segment
  //line 2 (x3,y3)--(x4,y4) is the xD 
  struct gpsr_neighbor *other = getnb(theother);

  if(other==NULL){
    printf("Wrong the other node\n");
    exit(1);
  }


  ****************

struct gpsr_neighbor*
GPSRNeighbors::getnb(nsaddr_t nid){
  struct gpsr_neighbor *temp = head_;
  while(temp){
    if(temp->id_ == nid){
      if((GPSR_CURRENT - temp->ts_) < DEFAULT_GPSR_TIMEOUT)
	return temp;
      else {
	delnb(temp); //if this entry expire, delete it and return NULL
	return NULL;
      }
      return temp;
    }
    temp = temp->next_;
  }
  return NULL;
}

the simulation scene was shown in the attachment.
when i make node 0 send packet to node 1, the error will occur.
would somebody know how to solve it?
thank you for your time!

Help To Disperse The Data Using Bitsplitter

I am totally new to Linux. I am working on Nubisave project in TU Dresden. I am stuck in running the C program. Its bitsplitter program. Can anybody help me out? should I post the program here? Will it be helpful for me? I knnow little basics of C language and the code is totally new for me.
I have used the code of bitsplitter and it is running but I am not understanding the output. This is code from Tu Dresden. Can anybody explain me how it is working?

#include "bitsplit.h"

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <stdlib.h>
#include <string.h>

#define SPLITDEBUG 0

struct fillbyte_t {
char *bits;
int bitctr;
char bytes[1024];
int bytectr;
unsigned char numbits;
unsigned char shiftbits;
unsigned int pattern;
FILE *f;
char *buffer;
ssize_t bufferctr;
};
typedef struct fillbyte_t fillbyte;

struct bitsplit_t {
fillbyte *b;
int parts;
int redundant;
int totalbits;
};
typedef struct bitsplit_t bitsplit;

static void bitsplitfinish(bitsplit bs)
{
int j;

for(j = 0; j < (bs.parts + bs.redundant); j++)
{
if(bs.b[j].f)
fclose(bs.b[j].f);
free(bs.b[j].bits);
/*free(bs.b[j].buffer);*/ /* the caller clears this */
}
free(bs.b);
bs.b = NULL;
}

static bitsplit bitsplitprepare(const char *directory, int parts, int redundant, int bitwidth)
{
fillbyte *b;
bitsplit bs;
int ret;
char fname[1024];
int sumbits, totalbits;
int i, j;

bs.b = NULL;

if(directory)
{
ret = mkdir(directory, S_IRWXU);
if((ret != 0) && (errno != EEXIST)) return bs;
}

b = malloc((parts + redundant) * sizeof(fillbyte));
for(j = 0; j < (parts + redundant); j++)
{
b[j].bitctr = 0;
b[j].bytectr = 0;
b[j].buffer = NULL;
b[j].bufferctr = 0;
if(directory)
{
snprintf(fname, sizeof(fname), "%s/%i.stream", directory, j);
b[j].f = fopen(fname, "w");
}
else b[j].f = NULL;
}

sumbits = 0;
totalbits = (int)(((parts - 1) / bitwidth) + 1) * bitwidth;
#if SPLITDEBUG
printf("bitwidth: %i; totalbits: %i\n", bitwidth, totalbits);
#endif

if(redundant)
if(totalbits % parts != 0)
{
bitsplitfinish(bs);
return bs;
}

for(j = parts - 1; j >= 0; j--)
{
b[j].bits = (char*)malloc(totalbits * 8 * sizeof(char));

b[j].numbits = (int)((totalbits - sumbits) / (j + 1));
b[j].pattern = 0;
for(i = sumbits; i < sumbits + b[j].numbits; i++)
b[j].pattern |= (1 << i);
b[j].shiftbits = sumbits;
sumbits += b[j].numbits;
#if SPLITDEBUG
printf("bits: fragment %i=%i, pattern=%u\n", j, b[j].numbits, b[j].pattern);
#endif
}
for(j = parts; j < parts + redundant; j++)
{
b[j].bits = (char*)malloc(totalbits * 8 * sizeof(char));
}

bs.b = b;
bs.parts = parts;
bs.redundant = redundant;
bs.totalbits = totalbits;

return bs;
}

static int bitjoinwork(const char **inbuffers, ssize_t *insizes, bitsplit bs)
{
ssize_t i;
int j, k;
int target, revtarget;
int topk, topkcandidate;
int outbits;
int shifted;
int smallest;
int rels[16];
int inshift;

outbits = 8;
shifted = 0;

int xspecial = 0;

xspecial = 1;
if(bs.totalbits > 2 * bs.parts)
xspecial = 0;
if(xspecial)
for(j = 0; j < bs.parts; j++)
if(bs.b[j].numbits > 8) xspecial = 0;
if((bs.parts == 2) && (bs.totalbits == 11))
xspecial = 1;

if((!xspecial) && (bs.totalbits > 8))
outbits = 16;

topk = 0;
for(j = 0; j < bs.parts; j++)
{
topkcandidate = (int)((float)outbits / bs.b[j].numbits) - 1;
if(topkcandidate > topk)
topk = topkcandidate;
}
for(j = 0; j < bs.parts; j++)
{
rels[j] = (int)((topk + 1) / (outbits / bs.b[j].numbits));
// FIXME: mostly untested: =2, *=2 etc. yields 89 OKs
if((!xspecial) && (bs.totalbits > 8))
rels[j] = 2;
if(bs.b[j].numbits == 4)
{
if((bs.parts == 2) && (bs.totalbits == 9))
{
rels[j] = 1;
topk = 1;
}
}
#if SPLITDEBUG
printf("REL/%i=%i <<%i>>\n", j, rels[j], outbits);
#endif
}

smallest = (int)(insizes[0] / rels[0]);
if(((int)(float)insizes[0] * 2 / rels[0]) != smallest * 2)
smallest++;

inshift = (outbits - (topk + 1) * bs.b[0].numbits) % outbits;

int special = 0;
if((bs.parts == 2) && (bs.totalbits > 8))
special = 1;
int special2 = 0;
if((bs.parts == 2) && (bs.totalbits == 5))
special2 = 1;
if((bs.parts == 2) && (bs.totalbits == 7))
special2 = 1;
if((bs.parts == 3) && (bs.totalbits == 7))
special2 = 1;
int special3 = 0;
if((bs.parts == 5) && (bs.totalbits == 15))
special3 = 1;
if((bs.parts == 4) && (bs.totalbits == 12))
special3 = 1;
if((bs.parts == 3) && (bs.totalbits == 9))
special3 = 1;
int special4 = 0;
if((bs.parts == 3) && (bs.totalbits == 15))
special4 = 1;

target = 0;
for(i = 0; i < smallest; i++)
{
#if SPLITDEBUG
printf("- reverse source-pos:%li\n", i);
#endif
// FIXME: special; not for w=14||12, only for w=10!
if((special) && (bs.totalbits == 10) && (shifted >= 10))
{
shifted -= bs.totalbits;
if(shifted < 0)
shifted = 0;
}
#if SPLITDEBUG
printf(" remaining target:%i\n", target);
#endif

for(k = topk; k >= 0; k--)
{
for(j = bs.parts - 1; j >= 0; j--)
{
int packval = (int)(unsigned char)inbuffers[j][i * rels[j]];
// FIXME: read-ahead depends on the relative difference between numbits?
if(rels[j] == 2)
packval = (packval << 8) + (int)(unsigned char)inbuffers[j][i * rels[j] + 1];
// FIXME: special
if((!special) && (!special2) && (!special3) && (!special4))
packval >>= inshift;

int packshift = k * bs.b[j].numbits;
// FIXME: special; only for w=14||12, not for w=10!
if(special)
{
if(bs.b[j].numbits != 4)
packshift = k * 8 - bs.b[j].numbits;
if(bs.totalbits != 10)
packshift += 8;

// for 5+4 bits
if((packshift == 20) && (bs.b[j].numbits == 4))
packshift = 12;
else if((packshift == 16) && (bs.b[j].numbits == 4))
packshift = 8;
else if((packshift == 12) && (bs.b[j].numbits == 4))
packshift = 4;
else if((packshift == 8) && (bs.b[j].numbits == 4))
packshift = 0;
}
if(special2)
{
if((packshift == 9) && (bs.b[j].numbits == 3))
packshift = 13;
else if((packshift == 6) && (bs.b[j].numbits == 3))
packshift = 10;
else if((packshift == 3) && (bs.b[j].numbits == 3))
packshift = 5;
else if((packshift == 0) && (bs.b[j].numbits == 3))
packshift = 2;
}
if(special3)
{
if(packshift == 12)
packshift = 13;
else if(packshift == 9)
packshift = 10;
else if(packshift == 6)
packshift = 5;
else if(packshift == 3)
packshift = 2;
else if(packshift == 0)
continue;
}
if(special4)
{
if(packshift == 10)
packshift = 11;
else if(packshift == 5)
packshift = 3;
else if(packshift == 0)
continue;
}
#if SPLITDEBUG
printf("PACKSHIFT:%i (on val %i) [filtered:%i] [shifted:%i]\n", packshift, packval, (bs.b[j].pattern >> bs.b[j].shiftbits), shifted);
#endif
revtarget = (packval >> packshift) & (bs.b[j].pattern >> bs.b[j].shiftbits);
#if SPLITDEBUG
printf(" -> reverse target @%i[pos:%i]:%u\n", j, k, revtarget);
#endif
target += revtarget << shifted;
shifted += bs.b[j].numbits;
#if SPLITDEBUG
printf(" => target: %i (shifted:%i)\n", target, shifted);
#endif
if(shifted >= outbits)
{
while(shifted >= 8)
{
// FIXME: special; only for w>=10?
if((special) && (shifted == bs.totalbits))
break;
#if SPLITDEBUG
printf("@%li: %i [extra]\n", bs.b[0].bufferctr, target & 0xFF);
#endif
if(bs.b[0].bufferctr % 1024 == 0)
bs.b[0].buffer = (char*)realloc(bs.b[0].buffer, (bs.b[0].bufferctr + 1024) * sizeof(char));
bs.b[0].buffer[bs.b[0].bufferctr] = target & 0xFF;
shifted -= 8;
bs.b[0].bufferctr++;
target = (target - (target & 0xFF)) >> 8;
}
}
}
}
}

return 0;
}

static int bitsplitwork(const char *inbuffer, ssize_t insize, bitsplit bs)
{
ssize_t i;
int j, k, l;
int target, shifted, inshifted;
int precounted;

fillbyte *b;
int totalbits;
int parts;
int redundant;

int outbits;
int stuffingthreshold;

b = bs.b;
totalbits = bs.totalbits;
parts = bs.parts;
redundant = bs.redundant;

outbits = 8;
inshifted = 0;
stuffingthreshold = totalbits * 8;

for(i = 0; i < insize; i++)
{
if(inshifted > 8)
{
inshifted -= 8;
i += 1;
}
#if SPLITDEBUG
printf("- source-pos:%li\n", i);
#endif
for(j = 0; j < parts; j++)
{
//target = inbuffer[i] & b[j].pattern;
target = (*(int*)(inbuffer + i)) & (b[j].pattern << inshifted);
target = target >> (b[j].shiftbits + inshifted);
#if SPLITDEBUG
printf(" -> target @%i[pos:%i]:%u\n", j, b[j].bitctr, target);
#endif

b[j].bits[b[j].bitctr] = target;
b[j].bitctr++;
if(b[j].bitctr == stuffingthreshold)
{
target = 0;
shifted = outbits - b[j].numbits;
precounted = 0;
for(k = 0; k < b[j].bitctr; k++)
{
#if SPLITDEBUG
printf("@%i: (%i->%i) >> %i ; << %i\n", j, b[j].numbits, b[j].bits[k], b[j].shiftbits, shifted);
#endif
target |= (b[j].bits[k] << shifted);
shifted -= b[j].numbits;
if(shifted < 0)
{
b[j].bytes[b[j].bytectr] = target & 0xFF;
b[j].bytectr++;
shifted = outbits - b[j].numbits;
precounted++;
#if SPLITDEBUG
printf("@%i: [bytectr++]: %i {%i}\n", j, target & 0xFF, target);
#endif
target = target >> outbits;
}
}
//b[j].bytectr += (int)((b[j].numbits * totalbits * 8 - 1) / 8) + 1 - precounted;
b[j].bytectr += (int)((totalbits - 1) / 8);
#if SPLITDEBUG
printf("@%i: stuffed %i 'bits'/fragments; bytectr now %i\n", j, totalbits, b[j].bytectr);
#endif
b[j].bitctr = 0;
if(b[j].bytectr >= 1024 - stuffingthreshold)
{
if((j == 0) && (redundant))
{
#if SPLITDEBUG
printf(">> redundancy\n");
#endif
b[parts].bytectr = b[j].bytectr;
for(l = 0; l < b[parts].bytectr; l++)
{
b[parts].bytes[l] = 0;
for(k = 0; k < parts; k++)
b[parts].bytes[l] ^= b[k].bytes[l];
}
if(b[parts].f)
fwrite(b[parts].bytes, 1, b[parts].bytectr, b[parts].f);
else
{
b[parts].buffer = (char*)realloc(b[parts].buffer, (b[parts].bufferctr + b[parts].bytectr) * sizeof(char));
memcpy(b[parts].buffer + b[parts].bufferctr, b[parts].bytes, b[parts].bytectr);
b[parts].bufferctr += b[parts].bytectr;
}
}
if(b[j].f)
fwrite(b[j].bytes, 1, b[j].bytectr, b[j].f);
else
{
b[j].buffer = (char*)realloc(b[j].buffer, (b[j].bufferctr + b[j].bytectr) * sizeof(char));
memcpy(b[j].buffer + b[j].bufferctr, b[j].bytes, b[j].bytectr);
b[j].bufferctr += b[j].bytectr;
}
b[j].bytectr = 0;
#if SPLITDEBUG
printf("@%i: reset bytectr\n", j);
#endif
}
}
}

i += (int)((totalbits - 1) / 8);
if((totalbits % 8) != 0)
{
inshifted += totalbits % 8;
if(inshifted % 8 == 0)
{
i += (inshifted - 8) / 8;
inshifted = 0;
}
else
i--;
}
}

for(j = 0; j < parts; j++)
{
if(b[j].bitctr > 0)
{
target = 0;
shifted = outbits - b[j].numbits;
precounted = 0;
for(k = 0; k < b[j].bitctr; k++)
{
#if SPLITDEBUG
printf("@%i: (%i=>%u) >> %i ; << %i\n", j, b[j].numbits, b[j].bits[k], b[j].shiftbits, shifted);
#endif
target |= (b[j].bits[k] << shifted);
shifted -= b[j].numbits;
if(shifted < 0)
{
b[j].bytes[b[j].bytectr] = target & 0xFF;
b[j].bytectr++;
shifted = outbits - b[j].numbits;
precounted++;
#if SPLITDEBUG
printf("@%i: [final-bytectr++]: %i {%i}\n", j, target & 0xFF, target);
#endif
target = target >> 8;
}
}
if(shifted != outbits - b[j].numbits)
{
#if SPLITDEBUG
printf("@%i: final-partialbyte: %i\n", j, target & 0xFF);
#endif
b[j].bytes[b[j].bytectr] = target & 0xFF;
b[j].bytectr++;
precounted++;
}
//b[j].bytectr += (int)((b[j].numbits * b[j].bitctr - 1) / totalbits) + 1 - precounted;
// FIXME: this may need a more general treatment
if(totalbits != 16)
b[j].bytectr += (int)((totalbits - 1) / 8);
#if SPLITDEBUG
printf("@%i: final-stuffed %i 'bits'/fragments; bytectr now %i\n", j, b[j].bitctr, b[j].bytectr);
#endif
b[j].bitctr = 0;
}
if(b[j].bytectr > 0)
{
if((j == 0) && (redundant))
{
#if SPLITDEBUG
printf(">> final-redundancy\n");
#endif
b[parts].bytectr = b[j].bytectr;
for(l = 0; l < b[parts].bytectr; l++)
{
b[parts].bytes[l] = 0;
for(k = 0; k < parts; k++)
b[parts].bytes[l] ^= b[k].bytes[l];
}
if(b[parts].f)
fwrite(b[parts].bytes, 1, b[parts].bytectr, b[parts].f);
else
{
b[parts].buffer = (char*)realloc(b[parts].buffer, (b[parts].bufferctr + b[parts].bytectr) * sizeof(char));
memcpy(b[parts].buffer + b[parts].bufferctr, b[parts].bytes, b[parts].bytectr);
b[parts].bufferctr += b[parts].bytectr;
}
}
if(b[j].f)
fwrite(b[j].bytes, 1, b[j].bytectr, b[j].f);
else
{
b[j].buffer = (char*)realloc(b[j].buffer, (b[j].bufferctr + b[j].bytectr) * sizeof(char));
memcpy(b[j].buffer + b[j].bufferctr, b[j].bytes, b[j].bytectr);
b[j].bufferctr += b[j].bytectr;
}
}
}

return 0;
}

int bitsplitbuffer(const char *inbuffer, ssize_t insize, char ***outbuffers, ssize_t **outsizes, int parts, int redundant, int bitwidth)
{
int ret;
int j;
bitsplit bs;

if((!inbuffer) || (parts <= 0)) return -1;
if(bitwidth < 1) return -1;

bs = bitsplitprepare(NULL, parts, redundant, bitwidth);
if(!bs.b) return -1;

*outbuffers = (char**)malloc((parts + redundant) * sizeof(char*));
*outsizes = (ssize_t*)malloc((parts + redundant) * sizeof(ssize_t));
for(j = 0; j < (parts + redundant); j++)
{
(*outbuffers)[j] = NULL;
(*outsizes)[j] = 0;
}

ret = bitsplitwork(inbuffer, insize, bs);

for(j = 0; j < (parts + redundant); j++)
{
(*outbuffers)[j] = bs.b[j].buffer;
(*outsizes)[j] = bs.b[j].bufferctr;
}

bitsplitfinish(bs);
return ret;
}

int bitjoinbuffer(const char **inbuffers, ssize_t *insizes, char **outbuffer, ssize_t *outsize, int parts, int bitwidth)
{
int ret;
bitsplit bs;

if((!inbuffers) || (parts <= 0)) return -1;

bs = bitsplitprepare(NULL, parts, 0, bitwidth);
if(!bs.b) return -1;

ret = bitjoinwork(inbuffers, insizes, bs);

*outbuffer = bs.b[0].buffer;
*outsize = bs.b[0].bufferctr;

bitsplitfinish(bs);
return ret;
}

int bitsplitfile(const char *filename, const char *directory, int parts, int redundant, int bitwidth)
{
FILE *f;
char buf[1024];
ssize_t size;
int ret;
bitsplit bs;

if((!filename) || (parts <= 0)) return -1;

f = fopen(filename, "r");
if(!f) return -1;

bs = bitsplitprepare(directory, parts, redundant, bitwidth);
if(!bs.b) return -1;

while(!feof(f))
{
size = fread(buf, 1, sizeof(buf), f);
#if SPLITDEBUG
printf("read %li bytes\n", size);
#endif

ret = bitsplitwork(buf, size, bs);
if(ret != 0)
{
fclose(f);
bitsplitfinish(bs);
return ret;
}
}

fclose(f);
bitsplitfinish(bs);
return 0;
}

int bitjoinfile(const char *filename, const char *directory, int parts, int bitwidth)
{
FILE *f, **dfs;
char **dbufs;
ssize_t *dsizes;
int ret;
//bitsplit bs;
int j;
char fname[1024];

if((!filename) || (parts <= 0)) return -1;

f = fopen(filename, "w");
if(!f) return -1;

dfs = (FILE**)malloc(parts * sizeof(FILE*));
dbufs = (char**)malloc(parts * sizeof(char**));
dsizes = (ssize_t*)malloc(parts * sizeof(ssize_t));

for(j = 0; j < parts; j++)
{
snprintf(fname, sizeof(fname), "%s/%i.stream", directory, j);
dfs[j] = fopen(fname, "r");
if(!dfs[j]) return -1;

dbufs[j] = (char*)malloc(1024 * sizeof(char));
}

//bs = bitsplitprepare(directory, parts, redundant, bitwidth);
//if(!bs.b) return -1;

while(!feof(dfs[0]))
{
for(j = 0; j < parts; j++)
{
dsizes[j] = fread(dbufs[j], 1, sizeof(dbufs[j]), dfs[j]);
#if SPLITDEBUG
printf("read %li bytes\n", dsizes[j]);
#endif
}

ret = -1;
//ret = bitsplitwork(buf, size, bs);
if(ret != 0)
{
for(j = 0; j < parts; j++)
fclose(dfs[j]);
fclose(f);
//bitsplitfinish(bs);
return ret;
}
}

for(j = 0; j < parts; j++)
fclose(dfs[j]);
fclose(f);
//bitsplitfinish(bs);
return 0;
}

please respond as soon as possible.

How To Avoid Newlines Output In Bash Script

I am have a bash script like this:

#!/bin/bash
while true;
do
(printf %s "$(date '+%A %W %Y %X')"; echo -n ", " ; printf %s "$(ps -eo pcpu,pmem,comm | grep statsd)") >> tope.txt;
sleep 2;
done

What I want is the output of this command be entered into tope.txt like this:

Friday 20 2015 11:28:21, 0.0 0.4 /usr/libexec/systemstatsd

but what I get is output like this in multiline. :

Friday 20 2015 11:28:21
,
0.0 0.4 /usr/libexec/systemstatsd

What can I do to fix this?

Basic Bash Script

Code:
PREFIX=192.168.1
OCTET=1
while [ $OCTET -lt "255" ]; do
    echo -en "Pinging ${PREFIX}.${OCTET}..."
    ping -c1 -w1 ${PREFIX}.${OCTET} > /dev/null 2>&1
    if [ "$?" -eq "0" ]; then
       echo "OK"
    else
       echo "Failed"
    fi
    let OCTET=$OCTET+1
done

My question is, in the penultimate line, why is it OCTET, and not $OCTET, with a dollar before it, meaning, the variable?

What if it were written:
Code:
let $OCTET=$OCTET+1

Like in C++ (I think) or Python or whatever?

Erreur When Install Ns2.34

Hi , i desinstall ns2.34 and i want to reinstall again so i was follow your step
$ tar xvf ns-allinone-2.34.tar.gz
$ cd ns-allinone-2.34/
$ patch -p0 < lwx+LTE+dccp+MPTCP.patch
$ export CC=gcc41 CXX=g++41 && ./install

and with this patch i add another patch for wimax "wimax-awg26_ns235.patch"

but when i install ns2.34 this erreur was showen
common/packet.h: In static member function ‘static void p_info::initName()’:
common/packet.h:402: error: ‘PT_WIMAXBS’ was not declared in this scope
make: *** [common/scheduler.o] Error 1
Ns make failed!

can you tell me where is the problem ???

Fg Error?

This is probably an easy fix, but it escapes me.

code:

D=date %Y-%m-%d
D1=`date +%s -d "$D"`
D2=`date +%s -d "2017-01-20"`
((diff_sec=D2-D1))
echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'0
echo ""

runtime:

$ ./datediff.sh
./datediff.sh: line 1: fg: no job control
Number of days : 656
$

eek.. ./datediff.sh: line 1: fg: no job control

Correctly Measure Available Memory In Linux

I'm currently using the command below, to output the memory usage of a debian system in bash scripts.

Code:
memusage=$(memusage=$(/usr/bin/free | grep Mem | awk '{print $3/$2 * 100.0 "%"}' | cut -d "%" -f1);echo "$(printf "%0.0f\n" $memusage)") && echo Memory Usage is $memusage

Being new to Linux though, I just found out that Linux takes up a lof of memory and puts it in cached.

Code:
root@Client1:~# free -m
             total       used       free     shared    buffers     cached
Mem:           927        799        128          0         15        754
-/+ buffers/cache:         28        898
Swap:           99          0         99

So the first command will actually output that the memory usage is 87%, when in reality is not. Its actually thinking that the memory usage is high because its also counting the cached memory, as used memory.

How can I modify the first command not to consider cached memory, as used memory?

Thanks

Lvalue Required As Left Operand Of Assignment

//I get error when I write this code
#include<iostream>
using namespace std;
float v[]={10,15,20,25,30,35,40};
float me(int i);
int main()
{
cout<<"Values before change\n";
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
me(3)=99;
me(4)=100;
cout<<"\nValue after change"<<endl;
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
return 0;
}
float me(int l)
{
return v[l];
}
//And when I use return by reference the error is gone
#include<iostream>
using namespace std;
float v[]={10,15,20,25,30,35,40};
float& me(int i);
int main()
{
cout<<"Values before change\n";
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
me(3)=99;
me(4)=100;
cout<<"\nValue after change"<<endl;
for(int i=0;i<5;i++)
{
cout<<"v["<<i<<"]="<<v[i]<<endl;
}
return 0;
}
float& me(int l)
{
return v[l];
}
//But I cannot Understand Why