Wednesday, April 13, 2011

How to determine the state of a process (i.e. if it is a zombie)

Hi,

how can I get information on the state of a process (i.e. if it is a zombie) using C under Linux?

After reading the answers so far I want to narrow my question somewhat: I would prefer a pure C solution. After reading the ps source (which reads /proc/) I thought that there should be a better way and asked here :)

From stackoverflow
  • I know only two ways:

    • Parsing output of the ps command
    • Reading files in /proc/PID, where PID is the process identifier (that's what ps does internally)
    simplyharsh : i think you should clarify a bit
  • Found here:

    Use this command to display all of your zombie processes:
    
    ps aux | awk '{ print $8 " " $2 }' | grep -w Z
    

    This could be easily parsed using C.

  • You want the processes running on your machine then use

    $ ps aux

    ps displays information about a selection of the active processes. If you want a repetitive update of the selection and the displayed information, use top instead.

    simplyharsh : yeah i guess TOP is a good idea. just need to be parsed in C.
    dmckee : I think "using C" means in a c program (i.e. not at the command prompt), and "under Linux" tells you what OS APIs he has access to.
    aatifh : @dmckee hehe I know that dude. :)
    aatifh : @taurean correct
  • You'll want to learn about interacting with the /proc/ "psuedo-filesystem" via typical C standard library calls. The documentation necessary to get started is included with any Linux distro and is a simple google search away.

    (Now that you know what to search for. I know that's usually most of the challenge!)

    In short, the directories and files within the /proc/ directory of a running Linux system reflect the state of the running kernel, which (naturally) includes processes. However, before you charge in you need to keep some information in mind.

    A zombie process isn't the same thing as an orphaned process. An orphaned process is a process left running in a waiting state after the process' parent has exited incorrectly. A zombie process is a process which has exited properly, released all its resources, but is maintaining a place in the process table.

    This typically happens when a process is launched by a program. You see, the kernel won't remove a finished sub-process' entry in the process table until the parent program properly fetches the return status of the sub-process. That makes sense; how else would the parent program know if the subprocess exited improperly?

    So all subprocesses are technically zombies for at least a very short time. It's not inherently a bad state for a program to be in.

    Indeed, "zombies" are sometimes created intentionally. For example, sometimes a zombie entry is left in place by a program for a while so that further launched processes won't get the same PID as the previously-launched (and now zombie) process.

    In other words, if you go SIGCHLDing zombie processes unnecessarily you might create a serious problem for the spawning program. However, if a process has been a zombie for a half hour or more, it's probably a sign of a bug.

    Edit: The question changed on me! No, there's no simpler way than how ps does it. If there was, it would have been integrated into ps a long time ago. The /proc files are the be-all-end-all source for information on the kernel's state. :)

    dmckee : It's a nice discussion of the task pitched for a less sophisticated audience
    Tilo Prütz : Your answer lead me to rethinking my approach of fixing a bug where the parent did not wait for the children properly. I reanalyzed the code and found the point where the parent missed to wait for the children. Thanks

Which is more efficient, PHP string functions or regex in PHP?

I'm writing PHP code to parse a string. It needs to be as fast as possible, so are regular expressions the way to go? I have a hunch that PHP string functions are more expensive, but it's just a guess. What's the truth?

Here's specifically what I need to do with the string:

Grab the first half (based on the third location of a substring "000000") and compare its hash to the next 20 bytes, throwing away anything left.

Parse the 9th byte through the next "000000" as one piece of data. Then grab the next 19 bytes after that, and split that into 8 (toss 1) and 8. Then I do some other stuff that converts those two 8 byte strings into dates.

So that's the kind of thing I need to do.

From stackoverflow
  • Native string functions are way faster. The benefit of regexp is that you can do pretty much anything with them.

  • I believe there is a threshold from which a regular expression is faster than a bunch of PHP string function calls. Anyway, depends a lot on what you're doing. You have to find out the balance.

    Now that you edited your question. I'd use string functions for what you're trying to accomplish. strpos() and substr() is what comes to mind at a first glance.

  • I think if you want highest performance, you should avoid regex as it helps to minimize effort, but won't have the best performance as you can almost always adjust code using string routines to a specific problem and gain a big performance boost of it. But for simple parsing routines that can't be optimized much, you can still use regex as it won't make a big difference there.

    EDIT: For this specific problem you posted I'd favorize string operations, but only because I wouldn't know how to do it in regex. This seems to be pretty straight-forward, except for the hash, so I think regex/string functions won't make a big difference.

  • It depends on your case: if you're trying to do something fairly basic (eg: search for a string, replace a substring with something else), then the regular string functions are the way to go. If you want to do something more complicated (eg: search for IP addresses), then the Regex functions are definitely a better choice.

    I haven't profiled regexes so I can't say that they'll be faster at runtime, but I can tell you that the extra time spent hacking together the equivalent using the basic functions wouldn't be worth it.


    Edit with the new information in the OP:

    It sounds as though you actually need to do a number of small string operations here. Since each one individually is quite basic, and I doubt you'd be able to do all those steps (or even a couple of those steps) at one time using a regex, I'd go with the basic functions:

    Grab the first half (based on the third location of a substring "000000") and compare its hash to the next 20 bytes, throwing away anything left.

    Use: strpos() and substr()
    Or : /$(.*?0{6}.*?0{6}.*?)0{6}/

    Then grab the next 19 bytes after that, and split that into 8 (toss 1) and 8.

    Use: substr() - (I assume you mean 17 bytes here -- 8 + 1 + 8)

    $part1 = substr($myStr, $currPos, 8);
    $part2 = substr($myStr, $currPos + 9, 8);
    
    troelskn : Regexp are surprisingly efficient. You shouldn't generally be afraid of using them as the default tool.
  • Depends on your needs. Most regular expression operations are faster than one would think and can even outperform builtin string functions in certain trivial operations. Note that I have the preg library in mind, not the builtin regex library, which is quite slow.

  • If what you're doing is at all reasonable to do using string functions, you should use them. Like, if you're determining whether a constant string 'abc' occurs in $value, you definitely want to check strpos($value, 'abc') !== false, not preg_match('/abc/', $value). If you find yourself doing a lot of string reshuffling and transformations in order to accomplish what you would've with a regex, though, you're almost certainly going to wind up destroying both performance and maintainability.

    When concerned about speed, though, when it comes down to it, don't think about it, clock it. The 'time' command is your friend.

  • In general, string functions are faster and regex functions are more flexible.

    As with anything else, your results may vary, the only way to know for sure is to try it both ways and benchmark.

Non-static const member, can't use default assignment operator

A program I'm expanding uses std::pair<> a lot.

There is a point in my code at which the compiler throws a rather large:

Non-static const member, 'const Ptr<double, double> std::pair<const Ptr<double, double>, const double*>::first' can't use default assignment operator

I'm not really sure what this is referring to? Which methods are missing from the Ptr class?

The original call that causes this problem is as follows:

vector_of_connections.pushback(pair(Ptr<double,double>,WeightValue*));
Where it's putting an std::Pair<Ptr<double,double>, WeightValue*> onto a vector, where WeightValue* is a const variable from about 3 fuctions back, and the Ptr<double,double> is taken from an iterator that works over another vector.

For future reference, Ptr is a pointer to a Node object.

From stackoverflow
  • At least mention which object the compiler is complaining about. Most probably you are missing a custom assignment member. If you don't have one, the default one kicks in. Probably, you also have a const member in that class (whose objects are being assigned) and since a const member cannot be changed you hit that error.

    Another approach: Since it's a class const, I suggest that you change it to a static const if that makes sense.

  • You have a case like this:

    struct sample {
        int const a; // const!
    
        sample(int a):a(a) { }
    };
    

    Now, you use that in some context that requires sample to be assignable - possible in a container (like a map, vector or something else). This will fail, because the implicitly defined copy assignment operator does something along this line:

    // pseudo code, for illustration
    a = other.a;
    

    But a is const!. You have to make it non-const. It doesn't hurt because as long as you don't change it, it's still logically const :) You could fix the problem by introducing a suitable operator= too, making the compiler not define one implicitly. But that's bad because you will not be able to change your const member. Thus, having an operator=, but still not assignable! (because the copy and the assigned value are not identical!):

        struct sample {
        int const a; // const!
    
        sample(int a):a(a) { }
    
        // bad!
        sample & operator=(sample const&) { }
    };
    

    However in your case, the apparent problem apparently lies within std::pair<A, B>. Remember that a std::map is sorted on the keys it contains. Because of that, you cannot change its keys, because that could easily render the state of a map invalid. Because of that, the following holds:

    typedef std::map<A, B> map;
    map::value_type <=> std::pair<A const, B>
    

    That is, it forbids changing its keys that it contains! So if you do

    *mymap.begin() = make_pair(anotherKey, anotherValue);
    

    The map throws an error at you, because in the pair of some value stored in the map, the ::first member has a const qualified type!

  • As far as I can tell, someplace you have something like:

    // for ease of reading 
    typedef std::pair<const Ptr<double, double>, const double*> MyPair;
    
    MyPair myPair = MAKEPAIR(.....);
    myPair.first = .....;
    

    Since the members of MyPair are const, you can't assign to them.

DataGridView Binding

I have a gridview that I am binding to via a generic list. I have set all the columns myself. I am just trying to:

Catch the event PRE format error when a row is edited- get the row information via a hidden field - and persist

I am sure this must be pretty easy but I haven't done much with forms work and I am unfamiliar with its DataGridViews Events.

From stackoverflow
  • There are two ways of looking at this;

    • handle the CellParsing event and parse the value
    • use a custom TypeConverter on the property

    I usually prefer the latter, since it takes this logic away from the UI; I'll see if I can do an example...


    Example (most of this code is the "show it working" code); here I define a MyDateTimeConverter, which formats/parses dates as their backwards "dd MMM yyyy" text (for no really good reason), and associate that converter with one of the properties. You can edit the values in the grid, and changes are pushed back in (change rows to see the "actual" value update). It doesn't show immediately because of some nuances around change-notification; it wasn't worth making the example more complex just for this...

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Globalization;
    using System.Windows.Forms;
    
    class Person
    {
        public string Forename { get; set; }
        public string Surname { get; set; }
    
        [TypeConverter(typeof(MyDateTimeConverter))]
        public DateTime EditableValue { get { return ActualValue; } set { ActualValue = value; } }
        // this just proves what we have set...
        public DateTime ActualValue { get; private set; }
    }
    class MyDateTimeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
        }
        const string FORMAT = "dd MMM yyyy";
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value != null && value is string)
            {
                string s = (string)value;
                return DateTime.ParseExact(Reverse(s), FORMAT, CultureInfo.InvariantCulture);
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return Reverse(((DateTime)value).ToString(FORMAT, CultureInfo.InvariantCulture));
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
        static string Reverse(string value)
        {
            char[] data = value.ToCharArray();
            Array.Reverse(data);
            return new string(data);
        }
    }
    class MyForm : Form
    {
        public MyForm()
        {
            DataGridView grid = new DataGridView();
            grid.Dock = DockStyle.Fill;
            List<Person> people = new List<Person>();
            people.Add(new Person { Forename = "Fred", Surname = "Flintstone", EditableValue = DateTime.Today });
            people.Add(new Person { Forename = "Barney", Surname = "Rubble", EditableValue = DateTime.Today.AddDays(-25) });
            grid.DataSource = people;
            Controls.Add(grid);
        }
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new MyForm());
        }
    }
    

How is the Spring MVC spring:bind tag working and what are the meanings of status.expression and status.value ?

Let's discuss on the following example:

<spring:bind path="user.userName">
    <input type="text" name="${status.expression}" value="${status.value}"/>
    <span class="fieldError">${status.errorMessage}</span>
</spring:bind>

When this view snippet gets rendered, what do ${status.expression} and ${status.value} get evaluated to? Where do these values come from?

From stackoverflow
  • See this link for an explanation of what the status variables mean.

    • status.expression: the expression that was used to retrieve the bean or property
    • status.value: the actual value of the bean or property (transformed using registered PropertyEditors)
    • status.errorMessages: an array of error messages, resulting from validation

    The status object is evaluated when the binding is done.

    Also have in mind that Spring 2.0 introduced new form tags, which are probable better suited for your needs.

    Koraktor : Thanks for the reminder that the value of `status.value` is generated by the registered PropertyEditor. I implemented a custom editor for setting a property and omitted the `getAsText()` method. This will result in `"null"` strings!

What is the difference between for..in and for each..in in javascript?

What is the difference between for..in and for each..in statements in javascript? Are there subtle difference that I don't know of or is it the same and every browser has a different name for it?

From stackoverflow
  • "for each...in" iterates a specified variable over all values of the specified object's properties.

    Example:

    var sum = 0;
    var obj = {prop1: 5, prop2: 13, prop3: 8};
    for each (var item in obj) {
      sum += item;
    }
    print(sum); // prints "26", which is 5+13+8
    

    Source

    "for...in" iterates a specified variable over all properties of an object, in arbitrary order.

    Example:

    function show_props(obj, objName) {
       var result = "";
       for (var i in obj) {
          result += objName + "." + i + " = " + obj[i] + "\n";
       }
       return result;
    }
    

    Source

    Vijay Dev : Is this browser specific ?
    Christoph : @Vijay: yes - it was introduced in JavaScript 1.6, ie a Mozilla extension
  • Read the excellent MDC documentation.

    The first is for normal looping over collections and arbitrarily over an object's properties.

    A for...in loop does not iterate over built-in properties. These include all built-in methods of objects, such as String's indexOf method or Object's toString method. However, the loop will iterate over all user-defined properties (including any which overwrite built-in properties).

    A for...in loop iterates over the properties of an object in an arbitrary order. If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

    The latter allows you to loop over an object's properties.

    Iterates a specified variable over all values of object's properties. For each distinct property, a specified statement is executed.

  • This demonstration should hopefully illustrate the difference.

    var myObj = {
        a : 'A',
        b : 'B',
        c : 'C'
    };
    for each (x in myObj) {
        alert(x);        // "A", "B", "C"
    }
    for (x in myObj) {
        alert(x);        // "a", "b", "c"
        alert(myObj[x]); // "A", "B", "C"
    }
    
  • In addition to the other answers, keep in mind that for each...in is not part of the ECMA standard and also isn't included in the upcoming edition 3.1. It was introduced in JavaScript 1.6, which is an extension of ECMAScript3 by the Mozilla Foundation.

    According to the linked Wikipedia page, it's only implemented in Firefox 1.5+ and Safari 3.x(+?).

    Crescent Fresh : In other words, it's "Firefox only".

Determine transfered data size for a web service call in .NET CF

Hi,

I'm developing a .NET CF client application and using web services for data transfer. I'm using SharpZipLib to compress transfered datasets so I know the size of the transfered byte array.

I wonder is there an easy way to determine to complete request size (html headerder, soap envelops and the real data) for a single call. I really want to minimize the GPRS connection costs.

Thanks...

From stackoverflow
  • Re the overall question; sorry, I don't know short of using a network tracer...

    However; can I humbly propose that datasets and SOAP are not always the best choice on bandwidth restricted devices? Compression does a good job, but not always ideal. Unless you need the features offered, simpler protocols are available (such as POX, perhaps using inbuilt protocol compression (GZIP/Deflate)).

    At the other end of things... if you can phrase things as messages, then serializers like protobuf-net might be useful (combined with raw binary posts); they are very data dense (such that attempts to use compression inevitably increases the size). However, you'd need to do your own data/change tracking at the client, and the RPC stack is as-yet incomplete (I've got working prototype code, but I haven't committed it yet, as I'm still unit testing it). The server would also be different (i.e. not an asmx or whatever - perhaps a rigged handler or MVC controller).

    As another alternative - ADO.NET Data Services might be of interest, especially in JSON mode (for bandwidth, again using protocol compression).

    xarux : You are right, may be web services is not a good choice but I don't think I have time to change all the structure. I also use db4o on client devices which supports server-client type communication but I'm not sure it support compression.
  • Wireshark is a famous protocol analyzer tool. However it may be an overkill for your needs.

    Also checkout Fiddler. This is easier and it will allow you to monitor traffic from an emulator.

    tcpmon is a Java utility that can sit between a server and a client. You need to edit the endpoint in your application to connect to tcpmon and configure tcpmon to proxy all requests to the actual web service. It shouldn't take more than 10 minutes - it is a very simple utility. Then you can monitor the raw requests in tcpmon or capture traffic with Fiddler.

    xarux : I have used Fiddler before but never tried Wireshark. From WM Emulator which connects to internet through Activesync Fiddler doesn't capture the traffic. But thanks for reminding these two.
  • WCF supports message tracing which would let you see the size of the generated SOAP+Message. You could use these trace files to determine what you are looking for although with compression on your communications the bytes sent will be less obviously. For the actual on the wire size wireshark would be a good bet. Or you could zip the message pulled from the WCF trace and get a rough idea.